js 实现链接跳转
使用 window.location.href 跳转
通过修改 window.location.href 属性实现页面跳转,这是最常用的方法:
window.location.href = 'https://example.com';
使用 window.location.replace 跳转
window.location.replace 方法会替换当前页面历史记录,用户无法通过后退按钮返回:
window.location.replace('https://example.com');
使用 window.open 打开新窗口
在新窗口或标签页中打开链接,可以指定窗口特性:
window.open('https://example.com', '_blank');
使用 location.assign 方法
location.assign 方法会保留当前页面历史记录,用户可以通过后退按钮返回:

location.assign('https://example.com');
使用 HTML 锚点标记
通过模拟点击 <a> 标签实现跳转:
const link = document.createElement('a');
link.href = 'https://example.com';
link.click();
使用 meta 标签跳转
通过设置 <meta> 标签实现自动跳转,常用于页面重定向:

<meta http-equiv="refresh" content="0;url=https://example.com">
使用表单提交跳转
通过提交表单实现跳转,可以携带参数:
const form = document.createElement('form');
form.method = 'GET';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用 history.pushState 跳转
通过 History API 改变 URL 而不刷新页面:
history.pushState({}, '', 'https://example.com');
使用 history.replaceState 跳转
替换当前历史记录而不刷新页面:
history.replaceState({}, '', 'https://example.com');
注意事项
不同跳转方法适用于不同场景,window.location.href 最常用且兼容性最好。需要考虑浏览器兼容性、SEO 影响和用户体验。






