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', '_self'); // 当前窗口
window.open('https://example.com', '_blank'); // 新窗口
使用 meta 标签实现跳转
在 HTML 中插入 meta 标签可以实现自动跳转,适合在服务端渲染的场景中使用。

const meta = document.createElement('meta');
meta.httpEquiv = 'refresh';
meta.content = '0;url=https://example.com';
document.head.appendChild(meta);
使用导航 API 实现跳转
现代浏览器支持 Navigation API,可以更灵活地控制页面跳转。
navigation.navigate('https://example.com');
使用表单提交实现跳转
通过动态创建表单并提交,可以模拟表单跳转行为。
const form = document.createElement('form');
form.method = 'GET';
form.action = 'https://example.com';
document.body.appendChild(form);
form.submit();
使用 history.pushState 实现无刷新跳转
history.pushState 可以修改 URL 而不刷新页面,适合单页应用(SPA)。

history.pushState({}, '', 'https://example.com');
使用 a 标签点击实现跳转
动态创建 a 标签并模拟点击事件,实现跳转。
const a = document.createElement('a');
a.href = 'https://example.com';
a.click();
使用框架跳转
如果页面包含 iframe,可以通过修改 iframe 的 src 属性实现跳转。
document.getElementById('iframeId').src = 'https://example.com';
使用服务端重定向
在 Node.js 等后端环境中,可以通过设置响应头实现跳转。
response.writeHead(302, { 'Location': 'https://example.com' });
response.end();






