怎么用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.open
在新标签页或窗口中打开页面,可通过参数控制打开方式:
window.open("https://example.com", "_blank"); // 新标签页
window.open("https://example.com", "_self"); // 当前标签页
使用 location.assign
与 href 效果相同,但语义更明确:
window.location.assign("https://example.com");
使用 meta 标签自动跳转
通过动态插入 <meta> 标签实现延迟跳转(需放在 <head> 中):
const meta = document.createElement("meta");
meta.httpEquiv = "refresh";
meta.content = "5;url=https://example.com"; // 5秒后跳转
document.head.appendChild(meta);
使用表单提交跳转
通过动态创建表单并提交实现跳转(适用于需要传递数据的场景):

const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
注意事项
- 使用
href或assign会保留历史记录,replace则不会。 - 部分浏览器可能拦截
window.open的弹出窗口。 - 跳转前可添加条件判断或异步操作(如确认弹窗):
if (confirm("确认跳转?")) { window.location.href = "https://example.com"; }






