js实现跳转
使用 window.location 跳转
通过修改 window.location.href 实现页面跳转,这是最常用的方法。
window.location.href = 'https://example.com';
使用 window.location.replace 跳转
replace 方法会替换当前历史记录,用户无法通过浏览器后退按钮返回前一页。
window.location.replace('https://example.com');
使用 window.open 跳转
在新窗口或标签页中打开链接,可以通过参数控制窗口行为。
window.open('https://example.com', '_blank');
使用 meta 标签跳转
通过动态添加 <meta> 标签实现自动跳转,常用于延迟跳转场景。
const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '5;url=https://example.com';
document.head.appendChild(meta);
使用 history.pushState 跳转
修改浏览器历史记录而不刷新页面,适用于单页应用(SPA)。
history.pushState({}, '', '/new-path');
使用表单提交跳转
动态创建表单并提交,适用于需要 POST 请求的跳转。
const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用锚点跳转
通过修改 location.hash 实现页面内锚点跳转。
window.location.hash = '#section-id';
使用导航 API 跳转
现代浏览器支持的 Navigation API,提供更精细的控制。
navigation.navigate('https://example.com');






