用js实现页面跳转
使用 window.location.href 方法
通过修改 window.location.href 属性实现页面跳转,这是最常见的方式:
window.location.href = "https://example.com";
使用 window.location.replace 方法
replace 方法会替换当前页面历史记录,用户无法通过浏览器后退按钮返回原页面:
window.location.replace("https://example.com");
使用 window.location.assign 方法
assign 方法与 href 类似,但以方法形式调用:
window.location.assign("https://example.com");
使用 window.open 方法
在新标签页或窗口中打开页面:
window.open("https://example.com", "_blank");
使用 HTML <a> 标签模拟点击
通过 JavaScript 创建并触发 <a> 标签的点击事件:
const link = document.createElement("a");
link.href = "https://example.com";
link.target = "_blank"; // 可选:在新标签打开
link.click();
使用 meta 标签实现自动跳转
通过 JavaScript 动态插入 <meta> 标签实现自动跳转(通常用于延时跳转):
const meta = document.createElement("meta");
meta.httpEquiv = "refresh";
meta.content = "3;url=https://example.com"; // 3秒后跳转
document.head.appendChild(meta);
使用 form 表单提交跳转
适用于需要提交数据的场景:

const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
注意事项
- 现代浏览器可能会拦截非用户触发的
window.open调用。 - 跨域跳转时需确保目标页面允许被嵌入或跳转。
- 使用
replace方法会清除当前页面的历史记录。
根据具体需求选择合适的方法,普通跳转推荐使用 window.location.href。






