js如何实现窗口跳转
使用 window.location.href 实现跳转
通过修改 window.location.href 属性可以直接跳转到新的 URL。这是最常见的方式,会触发页面刷新。
window.location.href = "https://example.com";
使用 window.location.replace 实现跳转
window.location.replace 会替换当前页面历史记录,用户无法通过浏览器后退按钮返回原页面。

window.location.replace("https://example.com");
使用 window.open 打开新窗口
window.open 可以在新窗口或标签页中打开 URL,支持指定窗口特性(如尺寸、工具栏等)。
window.open("https://example.com", "_blank");
使用 window.location.assign 实现跳转
window.location.assign 与 href 类似,但更显式地表明跳转意图,会保留历史记录。

window.location.assign("https://example.com");
使用 HTML <a> 标签模拟跳转
通过 JavaScript 动态创建或触发 <a> 标签的点击事件,适合需要 SEO 或渐进增强的场景。
const link = document.createElement("a");
link.href = "https://example.com";
link.target = "_blank";
link.click();
使用 meta 标签实现自动跳转
在 HTML 中通过 <meta http-equiv="refresh"> 实现定时跳转,无需 JavaScript。
<meta http-equiv="refresh" content="5; url=https://example.com">
注意事项
- 跨域限制:某些跳转方式可能受浏览器同源策略限制。
- 用户体验:避免滥用
window.open,可能被浏览器拦截。 - 历史记录:根据需求选择是否保留历史记录(
replace会清除当前记录)。






