通过js实现页面的跳转页面跳转页面
使用JavaScript实现页面跳转
使用window.location.href
通过修改window.location.href属性实现页面跳转:
window.location.href = 'https://example.com';
使用window.location.replace
replace方法会替换当前页面,不会在浏览器历史记录中留下痕迹:
window.location.replace('https://example.com');
使用window.location.assign
assign方法加载新页面并在浏览器历史记录中保留当前页面:
window.location.assign('https://example.com');
使用window.open
在新窗口或标签页中打开页面:
window.open('https://example.com', '_blank');
使用meta标签刷新
通过JavaScript动态创建meta标签实现跳转:
const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '0;url=https://example.com';
document.head.appendChild(meta);
使用表单提交
通过JavaScript动态提交表单实现跳转:
const form = document.createElement('form');
form.method = 'GET';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用history.pushState
结合pushState和replaceState实现单页应用内的跳转:
history.pushState({}, '', '/new-page');
注意事项
- 使用
window.location.href是最常见的方式 replace方法适用于不希望用户返回前页面的场景- 新窗口打开可能被浏览器拦截
- 单页应用路由通常使用
history API
以上方法可根据具体需求选择使用,大多数场景下window.location.href即可满足需求。







