通过js实现页面的跳转页面跳转
使用 window.location.href
通过修改 window.location.href 属性可以直接跳转到指定 URL。window.location.href = "https://example.com";
此方法会记录跳转前的页面到浏览器历史记录中,用户可通过后退按钮返回。
使用 window.location.replace
window.location.replace("https://example.com");
与 href 不同,replace 不会在历史记录中保留当前页面,用户无法通过后退按钮返回。
使用 window.open
在新窗口或标签页中打开页面:window.open("https://example.com", "_blank");
第二个参数 _blank 表示新标签页,若需在当前窗口打开可使用 _self。
使用 meta 标签自动跳转
在 HTML 的 <head> 中添加以下代码可实现定时跳转:<meta http-equiv="refresh" content="5;url=https://example.com">content 中的数字表示延迟时间(秒),之后跳转到指定 URL。
使用表单提交跳转
通过 JavaScript 动态提交表单实现跳转:
const form = document.createElement("form");
form.method = "GET";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用 history.pushState 或 replaceState
修改浏览器历史记录并跳转(不刷新页面,适用于单页应用):history.pushState({}, "", "new-page.html");replaceState 用法类似,但不会新增历史记录条目。
使用导航按钮事件
通过按钮点击触发跳转:

<button onclick="window.location.href='https://example.com'">跳转</button>
注意事项
- 某些浏览器可能拦截
window.open触发的弹窗,需用户主动允许。 - 单页应用(SPA)推荐使用路由库(如 React Router、Vue Router)而非直接修改
location。 - 跳转前可先检查目标 URL 有效性,避免无效请求。






