js实现页面跳转
使用 window.location.href
通过修改 window.location.href 属性实现跳转,这是最常见的方式。
window.location.href = 'https://example.com';
使用 window.location.replace
与 href 类似,但不会在浏览器历史记录中留下当前页面的记录。
window.location.replace('https://example.com');
使用 window.open
在新窗口或标签页中打开页面,可通过参数控制行为。
window.open('https://example.com', '_blank');
使用 location.assign
与 href 类似,但更显式地表明跳转意图。
window.location.assign('https://example.com');
使用 meta 标签自动跳转
通过 HTML 的 <meta> 标签实现自动跳转,适合纯前端场景。
<meta http-equiv="refresh" content="3;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 或 replaceState
修改浏览器历史记录并跳转,适合单页应用(SPA)。
history.pushState({}, '', '/new-page');
使用导航 API(实验性)
现代浏览器支持的 Navigation API,适用于高级路由控制。

navigation.navigate('https://example.com');
注意事项
- 跳转前可检查
confirm或异步逻辑。 - 部分方法可能受浏览器安全策略限制(如弹窗拦截)。
- 单页应用推荐使用路由库(如 React Router、Vue Router)。






