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 可以在新标签页或窗口中打开 URL。可以指定窗口参数,如宽度、高度等。

window.open('https://example.com', '_blank');
使用 HTML 的 meta 标签
在 HTML 的 <head> 中添加 <meta> 标签,可以设置页面自动跳转。适合静态页面或需要延迟跳转的情况。
<meta http-equiv="refresh" content="5;url=https://example.com">
使用表单提交
通过 JavaScript 动态提交表单,可以实现跳转。适合需要传递表单数据的场景。

document.getElementById('myForm').action = 'https://example.com';
document.getElementById('myForm').submit();
使用 JavaScript 的 navigate 方法
现代浏览器支持 navigate 方法,提供更灵活的跳转选项。
window.navigate('https://example.com');
使用 history.pushState 或 history.replaceState
history.pushState 和 history.replaceState 可以修改浏览器历史记录而不刷新页面。适合单页应用(SPA)中的路由跳转。
history.pushState({}, '', 'https://example.com');
使用锚点跳转
通过修改 window.location.hash 可以实现页面内的锚点跳转。
window.location.hash = '#section1';






