js实现页面跳转
使用 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.open
在新窗口或标签页中打开页面:
window.open('https://example.com', '_blank');
第二个参数 _blank 表示在新标签页打开,可替换为 _self 在当前窗口打开。

使用 meta 标签自动跳转
在 HTML 中插入 meta 标签实现自动跳转:
document.head.innerHTML += '<meta http-equiv="refresh" content="3;url=https://example.com">';
content 中的数字表示延迟跳转的秒数。
使用表单提交跳转
通过动态创建表单实现跳转:

const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
适用于需要提交数据的 POST 请求跳转。
使用 history.pushState
通过 History API 修改 URL 而不刷新页面:
history.pushState({}, '', '/new-page');
适用于单页应用(SPA)的路由跳转,需配合路由库使用。
使用 location.assign
调用 location.assign 方法实现跳转:
location.assign('https://example.com');
效果与直接修改 href 类似,但可读性更好。






