jquery url
jQuery 获取当前 URL
使用 jQuery 获取当前页面的完整 URL 可以通过 window.location 对象实现。虽然 jQuery 本身不直接提供 URL 操作的方法,但可以结合原生 JavaScript 实现。
var currentUrl = window.location.href;
console.log(currentUrl);
jQuery 解析 URL 参数
如果需要从 URL 中提取查询参数(如 ?id=123),可以通过以下方式解析:

function getUrlParameter(name) {
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(window.location.search);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
// 使用示例:获取 URL 中的 id 参数
var id = getUrlParameter('id');
jQuery 修改 URL 不刷新页面
使用 history.pushState 可以修改 URL 而不触发页面刷新:
// 修改 URL 并添加历史记录
history.pushState({}, '', '/new-path');
// 替换当前 URL 不添加历史记录
history.replaceState({}, '', '/updated-path');
jQuery 监听 URL 变化
通过监听 popstate 事件可以响应浏览器前进/后退操作导致的 URL 变化:

$(window).on('popstate', function() {
console.log('URL changed to:', window.location.href);
});
jQuery 获取 URL 各部分
分解 URL 的各个组成部分(协议、主机、路径等):
var protocol = window.location.protocol; // "https:"
var host = window.location.host; // "example.com:8080"
var pathname = window.location.pathname; // "/path/page.html"
var search = window.location.search; // "?query=string"
var hash = window.location.hash; // "#section"
jQuery 重定向到新 URL
实现页面跳转的几种方式:
// 方法1:直接修改 location
window.location.href = "https://example.com";
// 方法2:使用 replace(不保留历史记录)
window.location.replace("https://example.com");
// 方法3:模拟点击链接
$('<a>', { href: 'https://example.com' })[0].click();






