js怎么实现网页跳转页面跳转
使用 window.location.href
通过修改 window.location.href 属性实现跳转,浏览器会加载新页面并保留历史记录:
window.location.href = "https://example.com";
使用 window.location.replace
跳转到新页面但不保留当前页面的历史记录(无法通过“后退”返回):
window.location.replace("https://example.com");
使用 window.open
在新标签页或窗口中打开页面(受浏览器弹窗策略限制):
window.open("https://example.com", "_blank");
使用 location.assign
与 window.location.href 类似,显式调用跳转方法:
location.assign("https://example.com");
使用 <a> 标签模拟点击
动态创建 <a> 标签并触发点击事件,适合需要模拟用户行为的场景:
const link = document.createElement("a");
link.href = "https://example.com";
link.target = "_blank";
link.click();
使用 meta 标签自动跳转
通过 HTML <meta> 标签实现页面延迟跳转(通常用于静态页面):
<meta http-equiv="refresh" content="5;url=https://example.com">
使用 history.pushState 或 replaceState
修改浏览器历史记录但不触发页面跳转(适用于单页应用 SPA):
history.pushState({}, "", "/new-page");
注意事项
- 跳转前可监听事件或验证条件,例如:
if (confirm("确认跳转?")) { window.location.href = "https://example.com"; } - 部分方法可能受浏览器安全策略限制(如
window.open在用户未交互时可能被拦截)。







