js实现打开页面
使用 window.open() 方法
通过 window.open() 可以打开新窗口或标签页。语法如下:
window.open(url, target, windowFeatures);
url:要加载的页面地址(可选,默认为空白页)。target:指定打开方式(如_blank新标签页,_self当前窗口)。windowFeatures:控制窗口样式的字符串(如宽度、高度)。
示例:
// 在新标签页打开
window.open('https://example.com', '_blank');
// 指定窗口尺寸
window.open('https://example.com', '_blank', 'width=600,height=400');
修改 window.location 实现页面跳转
直接修改 window.location 属性可在当前窗口打开新页面:
// 方式1:赋值完整URL
window.location.href = 'https://example.com';
// 方式2:使用assign方法(可回退)
window.location.assign('https://example.com');
// 方式3:替换当前历史记录(不可回退)
window.location.replace('https://example.com');
通过超链接模拟点击
动态创建 <a> 标签并触发点击事件:
const link = document.createElement('a');
link.href = 'https://example.com';
link.target = '_blank'; // 可选:新标签页
link.click();
使用 iframe 嵌入页面
在页面内嵌入其他页面内容:

const iframe = document.createElement('iframe');
iframe.src = 'https://example.com';
document.body.appendChild(iframe);
注意事项
- 浏览器可能拦截
window.open()的弹窗,需确保由用户操作(如点击按钮)触发。 - 跨域链接可能受安全策略限制。
- 移动端设备可能对窗口特性(如尺寸)支持有限。






