js实现页面的跳转
使用window.location.href进行跳转
通过修改window.location.href属性可以直接跳转到指定URL。这是最常用的页面跳转方法,会触发浏览器刷新并加载新页面。
window.location.href = 'https://example.com';
使用window.location.replace实现替换跳转
window.location.replace方法会替换当前页面在历史记录中的位置,用户无法通过浏览器后退按钮返回原页面。
window.location.replace('https://example.com');
使用window.open打开新窗口
需要在新窗口或标签页中打开页面时,可以使用window.open方法。可以指定窗口特性参数。

window.open('https://example.com', '_blank');
使用location.assign方法跳转
location.assign方法会加载新文档并在历史记录中保留原页面,用户可以通过后退按钮返回。
window.location.assign('https://example.com');
使用meta标签实现自动跳转
在HTML的head部分添加meta标签可以实现页面自动跳转,适合简单的重定向场景。

<meta http-equiv="refresh" content="5;url=https://example.com">
使用history.pushState实现无刷新跳转
需要改变URL但不刷新页面时,可以使用History API的pushState方法。适合单页应用(SPA)场景。
history.pushState({}, '', '/new-path');
使用form表单提交跳转
通过JavaScript动态创建并提交表单可以实现带参数的页面跳转。
const form = document.createElement('form');
form.method = 'POST';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用a标签模拟点击
通过编程方式触发a标签的点击事件来实现跳转,保持与用户手动点击一致的行为。
const link = document.createElement('a');
link.href = 'https://example.com';
link.click();






