实现js页面跳转
使用 window.location.href
通过修改 window.location.href 属性实现跳转,适用于大多数场景:
window.location.href = "https://example.com";
使用 window.location.replace
跳转且不保留当前页面历史记录,用户无法通过浏览器后退按钮返回:
window.location.replace("https://example.com");
使用 window.location.assign
与 href 类似,但以方法形式调用:

window.location.assign("https://example.com");
使用 window.open
在新标签页或窗口中打开链接,可通过参数控制行为:
window.open("https://example.com", "_blank");
使用 HTML <a> 标签模拟
动态创建 <a> 标签并触发点击,适合需要模拟用户交互的场景:

const link = document.createElement("a");
link.href = "https://example.com";
link.target = "_blank";
link.click();
使用 meta 标签自动跳转
通过 <meta http-equiv="refresh"> 实现定时跳转:
<meta http-equiv="refresh" content="5; url=https://example.com">
使用 history.pushState 或 replaceState
修改浏览器历史记录但不触发页面跳转,需配合路由框架使用:
history.pushState({}, "", "/new-path");
注意事项
- 跳转外部链接需确保协议完整(如
https://)。 - 部分浏览器可能限制
window.open的弹出行为。 - 使用
replace或pushState时需注意用户导航体验。






