实现js页面跳转页面跳转页面
使用 window.location.href
通过修改 window.location.href 属性实现跳转,这是最常用的方法:
window.location.href = "https://example.com";
使用 window.location.replace
replace 方法会替换当前页面,不会在历史记录中留下痕迹:
window.location.replace("https://example.com");
使用 window.open
在新窗口或标签页中打开页面,可通过参数控制行为:
window.open("https://example.com", "_blank");
使用 meta 标签自动跳转
在 HTML 头部添加 meta 标签实现自动跳转:
<meta http-equiv="refresh" content="5;url=https://example.com">
使用表单提交跳转
通过动态创建表单实现跳转:
const form = document.createElement("form");
form.method = "POST";
form.action = "https://example.com";
document.body.appendChild(form);
form.submit();
使用 history.pushState
在不刷新页面的情况下修改 URL:
history.pushState({}, "", "https://example.com");
使用锚点跳转
通过修改 hash 实现页面内跳转:
window.location.hash = "#section1";
使用 iframe 跳转
在 iframe 中加载目标页面:
document.getElementById("myFrame").src = "https://example.com";
使用导航 API
现代浏览器支持的 Navigation API:
navigation.navigate("https://example.com");
使用服务端跳转
通过服务器响应头实现跳转(需后端支持):

HTTP/1.1 302 Found
Location: https://example.com
每种方法适用于不同场景,可根据具体需求选择最合适的实现方式。






