js实现打开页面
使用 window.open() 方法
通过 window.open() 可以打开新页面或标签页。
window.open('https://example.com', '_blank');
参数说明:
- 第一个参数是目标 URL。
- 第二个参数指定打开方式(
_blank为新标签页,_self为当前窗口)。
使用 location.href 跳转
直接修改 location.href 实现页面跳转。
location.href = 'https://example.com';
等价于用户点击链接,会覆盖当前页面。

通过 <a> 标签模拟点击
动态创建 <a> 标签并触发点击事件。
const link = document.createElement('a');
link.href = 'https://example.com';
link.target = '_blank';
link.click();
适合需要控制打开逻辑的场景。
使用 location.replace() 替换当前页面
跳转但不保留当前页面的历史记录。

location.replace('https://example.com');
适用于不希望用户返回前页的情况。
表单提交跳转
通过动态表单提交实现页面跳转。
const form = document.createElement('form');
form.action = 'https://example.com';
form.method = 'POST';
document.body.appendChild(form);
form.submit();
适合需要 POST 请求的场景。
注意事项
- 浏览器可能拦截
window.open()的弹窗,需确保由用户操作触发。 - 跨域链接需遵循同源策略。
- 移动端可能对跳转行为有额外限制。






