用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");
使用 meta 标签自动跳转
在 HTML 的 <head> 中添加 meta 标签实现自动跳转,适合纯前端场景:
<meta http-equiv="refresh" content="5;url=https://example.com">
使用表单提交跳转
动态创建表单并提交,适合需要传递参数的场景:

const form = document.createElement('form');
form.method = 'GET';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用 History API
通过 history.pushState 或 history.replaceState 修改 URL 而不刷新页面,适合单页应用(SPA):
history.pushState({}, '', '/new-page');
使用导航按钮模拟
触发浏览器的前进/后退功能,适用于 SPA 中的路由控制:
history.back(); // 后退
history.forward(); // 前进
使用锚点跳转
跳转到当前页面的指定锚点位置:
window.location.hash = "#section1";
每种方法适用于不同场景,可根据实际需求选择。对于普通页面跳转,window.location.href 是最常用的方式;单页应用推荐使用 History API;需要传递参数时表单提交更合适。






