js引入js 实现跳转
使用 JavaScript 实现页面跳转
在 JavaScript 中,可以通过多种方式实现页面跳转。以下是几种常见的方法:
使用 window.location.href
通过修改 window.location.href 属性,可以实现页面跳转:
window.location.href = "https://example.com";
使用 window.location.replace
window.location.replace 方法会替换当前页面,不会在浏览器历史记录中留下记录:

window.location.replace("https://example.com");
使用 window.location.assign
window.location.assign 方法会加载新页面,并在浏览器历史记录中保留当前页面:
window.location.assign("https://example.com");
使用 window.open
window.open 方法可以在新窗口或标签页中打开页面:

window.open("https://example.com", "_blank");
使用 HTML <a> 标签模拟点击
通过 JavaScript 模拟点击 <a> 标签实现跳转:
const link = document.createElement("a");
link.href = "https://example.com";
link.click();
使用 meta 标签实现自动跳转
通过动态添加 <meta> 标签实现自动跳转:
const meta = document.createElement("meta");
meta.httpEquiv = "refresh";
meta.content = "0;url=https://example.com";
document.head.appendChild(meta);
注意事项
- 使用
window.location.replace时,用户无法通过浏览器后退按钮返回上一页。 window.open可能会被浏览器拦截,尤其是在用户未主动触发的情况下。- 动态添加
<meta>标签的方式适用于简单的自动跳转场景。
以上方法可以根据具体需求选择使用。






