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');
使用 HTML 锚点跳转
通过动态创建 <a> 标签并触发点击实现跳转,适合需要模拟用户点击的场景:

const link = document.createElement('a');
link.href = 'https://example.com';
link.click();
使用 meta 标签刷新
通过动态插入 <meta> 标签实现自动跳转,常用于延迟跳转:
const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '0;url=https://example.com';
document.head.appendChild(meta);
使用 history.pushState
单页应用(SPA)中无刷新修改 URL,通常配合路由库使用:
history.pushState({}, '', '/new-path');
使用表单提交
通过动态创建表单并提交实现跳转,适合需要 POST 请求的场景:
const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
注意事项
- 现代前端框架(如 React/Vue)通常使用专用路由库(react-router/vue-router)管理跳转
- 跨域跳转可能受浏览器安全策略限制
- 部分方法可能被广告拦截插件阻止






