通过js实现页面的跳转页面
使用 window.location.href 实现跳转
通过修改 window.location.href 属性可以直接跳转到目标页面。这是最常见的方式,适用于大多数场景。
window.location.href = 'https://example.com';
使用 window.location.replace 实现跳转
window.location.replace 会替换当前页面,不会在浏览器历史记录中留下痕迹。适合不希望用户返回上一页的场景。
window.location.replace('https://example.com');
使用 window.open 实现新窗口跳转
如果需要在新窗口或标签页中打开页面,可以使用 window.open。可以指定窗口特性(如尺寸、工具栏等)。
window.open('https://example.com', '_blank');
使用 location.assign 实现跳转
location.assign 与 window.location.href 类似,但更明确地表示跳转行为。
location.assign('https://example.com');
使用 meta 标签实现自动跳转
如果是纯前端需求,可以通过动态插入 <meta> 标签实现自动跳转。常用于页面重定向。
const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '0;url=https://example.com';
document.head.appendChild(meta);
使用 history.pushState 实现无刷新跳转
适用于单页应用(SPA),通过修改浏览器历史记录实现无刷新跳转。需配合路由库使用。

history.pushState({}, '', '/new-page');
注意事项
- 跳转前可检查目标地址的有效性,避免无效跳转。
- 使用
window.open时需注意浏览器可能会拦截弹出窗口。 - 单页应用推荐使用路由库(如 React Router、Vue Router)管理跳转逻辑。






