js如何实现窗口跳转
使用 window.location.href 实现跳转
直接修改 window.location.href 属性,浏览器会立即跳转到新 URL:
window.location.href = 'https://example.com';
使用 window.location.replace 实现跳转(无历史记录)
替换当前页面,不会在浏览器历史记录中生成新条目:

window.location.replace('https://example.com');
使用 window.open 打开新窗口
在新标签页或窗口中打开 URL,可通过参数控制窗口行为:
window.open('https://example.com', '_blank');
使用 location.assign 方法跳转
与直接修改 href 类似,但更显式地表明跳转意图:

location.assign('https://example.com');
使用 HTML 锚点模拟跳转
动态创建 <a> 标签并触发点击事件:
const link = document.createElement('a');
link.href = 'https://example.com';
link.target = '_blank';
link.click();
使用 meta 标签实现自动跳转
通过插入 <meta> 标签实现延时跳转(需在 <head> 中):
const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '3;url=https://example.com';
document.head.appendChild(meta);
注意事项
- 跨域限制:某些跳转方法可能受浏览器同源策略限制
- 弹窗拦截:
window.open可能被浏览器弹窗拦截器阻止 - 移动端兼容性:部分方法在移动设备上的表现可能与桌面端不同






