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="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 或 replaceState
适用于单页应用(SPA),仅更新 URL 而不刷新页面。
history.pushState({}, "", "/new-page");
使用导航 API(实验性)
现代浏览器支持的 Navigation API,适用于 SPA。

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






