怎么用js实现页面跳转
使用 window.location.href
设置 window.location.href 为目标 URL,浏览器会立即跳转:
window.location.href = "https://example.com";
使用 window.location.replace
与 href 类似,但不会在历史记录中留下当前页面的记录:

window.location.replace("https://example.com");
使用 window.open
在新窗口或标签页中打开目标 URL,可通过参数控制行为:
window.open("https://example.com", "_blank");
使用 meta 标签自动跳转
通过 HTML 的 <meta> 标签实现定时跳转(通常用于 SEO 或重定向场景):

<meta http-equiv="refresh" content="5; url=https://example.com">
使用表单提交跳转
动态创建表单并提交,适用于需要传递数据的场景:
const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用导航 API(现代浏览器)
通过 window.navigation.navigate 实现(需浏览器支持):
window.navigation.navigate("https://example.com");
注意事项
- 跳转前可添加条件判断或异步操作。
- 部分方法可能被浏览器弹窗拦截策略限制。
- 考虑兼容性,优先使用
window.location.href。






