怎么用js实现页面跳转
使用 window.location.href
通过修改 window.location.href 属性实现跳转,这是最常见的方式。直接赋值为目标 URL 即可跳转:
window.location.href = "https://example.com";
使用 window.location.replace
与 href 类似,但会替换当前页面历史记录,用户无法通过浏览器后退按钮返回原页面:
window.location.replace("https://example.com");
使用 window.location.assign
功能与 href 相同,但以方法形式调用,适合链式操作:

window.location.assign("https://example.com");
使用 window.open
在新标签页或窗口中打开链接,可通过参数控制行为:
window.open("https://example.com", "_blank"); // 新标签页
window.open("https://example.com", "_self"); // 当前标签页
使用 meta 标签自动跳转
通过 HTML 的 <meta> 标签实现定时跳转,适合静态页面:

<meta http-equiv="refresh" content="5; url=https://example.com">
使用表单提交跳转
动态创建表单并提交,适合需要传递数据的场景:
const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用 history.pushState 或 replaceState
修改浏览器历史记录但不跳转,需配合其他逻辑实现单页应用(SPA)路由:
history.pushState({}, "", "/new-page"); // 添加历史记录
history.replaceState({}, "", "/new-page"); // 替换当前记录
注意事项
- 跳转前可检查 URL 合法性,避免安全风险。
- 单页应用推荐使用
pushState或前端路由库(如 React Router、Vue Router)。 - 部分浏览器可能拦截弹窗(
window.open),需确保用户主动触发。






