js实现网页跳转
使用 window.location.href
通过修改 window.location.href 实现跳转,是最常见的方法。直接将目标 URL 赋值给该属性即可:
window.location.href = "https://example.com";
使用 window.location.replace
replace 方法会替换当前页面在历史记录中的位置,用户无法通过后退按钮返回原页面:
window.location.replace("https://example.com");
使用 window.open
在新窗口或标签页中打开链接,可通过参数控制打开方式:
window.open("https://example.com", "_blank"); // 新标签页
window.open("https://example.com", "_self"); // 当前窗口
使用 meta 标签自动跳转
通过动态创建 <meta> 标签实现自动跳转,适合简单的页面重定向:
const meta = document.createElement("meta");
meta.httpEquiv = "refresh";
meta.content = "0;url=https://example.com";
document.head.appendChild(meta);
使用表单提交跳转
通过 JavaScript 提交表单实现跳转,适合需要传递参数的场景:
const form = document.createElement("form");
form.method = "GET";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用 history.pushState
适用于单页应用(SPA)的路由跳转,不会真正加载新页面:

history.pushState({}, "", "/new-page");
注意事项
- 跨域限制:某些跳转方法可能受浏览器同源策略限制。
- SEO 影响:部分跳转方式(如 JavaScript 跳转)可能对搜索引擎不友好。
- 用户体验:避免频繁或突然的页面跳转,可能影响用户操作流程。






