js实现打开链接
使用 window.open 方法
通过 window.open 方法可以在新窗口或当前窗口打开链接。
window.open('https://example.com', '_blank'); // 新窗口打开
window.open('https://example.com', '_self'); // 当前窗口打开
修改 window.location
直接修改 window.location.href 可以在当前窗口跳转链接。
window.location.href = 'https://example.com'; // 当前页跳转
window.location.replace('https://example.com'); // 替换当前历史记录
使用 <a> 标签模拟点击
动态创建 <a> 标签并触发点击事件,适合需要控制行为的场景。
const link = document.createElement('a');
link.href = 'https://example.com';
link.target = '_blank'; // 新窗口打开
link.click();
表单提交跳转
通过表单提交实现跳转,适用于需要传递数据的场景。

const form = document.createElement('form');
form.method = 'GET';
form.action = 'https://example.com';
form.target = '_blank'; // 新窗口打开
document.body.appendChild(form);
form.submit();
注意事项
- 浏览器可能拦截
window.open触发的弹窗,需在用户交互事件(如点击)中调用。 _blank参数指定新窗口打开,_self为当前窗口。- 使用
replace()方法时,浏览器不会记录跳转前的页面历史。






