js实现url跳转
使用 window.location.href 实现跳转
通过修改 window.location.href 可以直接跳转到目标 URL。这是最常见的方法,会触发页面刷新并加载新页面。
window.location.href = 'https://example.com';
使用 window.location.replace 实现跳转(无历史记录)
window.location.replace 会跳转到新 URL,但不会在浏览器历史记录中留下当前页面的记录,用户无法通过“后退”按钮返回。

window.location.replace('https://example.com');
使用 window.location.assign 实现跳转
window.location.assign 与 href 类似,会跳转并保留历史记录,但更显式地表明跳转意图。
window.location.assign('https://example.com');
使用 window.open 在新标签页打开 URL
通过 window.open 可以在新标签页或窗口中打开目标 URL。第二个参数可指定窗口名称(如 _blank),第三个参数可配置窗口属性。

window.open('https://example.com', '_blank');
使用 meta 标签自动跳转(HTML 方式)
在 HTML 中插入 <meta> 标签,通过 http-equiv="refresh" 实现自动跳转,适合纯前端场景。
<meta http-equiv="refresh" content="0; url=https://example.com">
使用 navigateTo(框架特定方法)
在 Vue、React 等框架中,可使用路由提供的跳转方法。例如 Vue Router 的 router.push 或 React Router 的 useNavigate。
// Vue Router
router.push('/target-path');
// React Router
const navigate = useNavigate();
navigate('/target-path');
注意事项
- 跳转前可检查 URL 合法性,避免安全风险。
- 部分浏览器可能拦截弹出窗口,需处理用户交互触发。
- 框架路由跳转通常更高效,避免整页刷新。






