在 Microsoft Edge 中打开跨源 window 时忽略 window.open() 参数
window.open() parameters ignored when opening cross-origin window in Microsoft Edge
在 Microsoft Edge 中,当 url
与当前域的来源不同时,以下代码片段将忽略传递给 window.open()
的选项。但它在 Chrome 和 Firefox 中工作正常,并且当域是同源时。
const popupWindow = window.open(
url,
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
我找到了一个让 window 正确调整大小的 hacky 解决方法,但这真的很烦人,你可以看到它重定向了页面。
const popupWindow = window.open(
"/#",
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
popupWindow.location.href = url;
我如何在 Microsoft Edge 上使用它?为什么 Microsoft Edge 忽略我的大小和其他参数?这是一个错误吗?
对我来说,它工作正常,所以问题可能出在您使用的 Edge 版本上。您可以使用 caniuse.com 来检查您是否可以使用东西。但是,由于您发现了这种使用它的 hacky 方式,您可以将它变成这样的函数:
function popup(url, title){
const popupWindow = window.open(
"/#",
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
popupWindow.location.href = url;
}
现在您可以使用弹出功能了。
对了,我看你用的是ES6。确保您的 Edge 版本支持 const。
我尝试检查问题,它看起来像是与安全设置相关的问题。
我建议您修改以下 Internet 选项 设置。
(1) 启用 跨域访问数据源 选项。
Internet Options -> Security (Tab) -> Custom Level -> Miscellaneous
-> Access data sources across domains -> Set to Enabled
(2) 禁用 保护模式.
Internet Options -> Security (Tab) -> uncheck Enable Protected mode
for Internet & Local Intranet
(3) 将两个域都添加到受信任的站点列表。
Internet Options -> Security (Tab) -> Trusted sites -> Sites -> Add both domains to the list.
(4) 取消勾选需要服务器验证(https:):
Internet Options -> Security (Tab) -> Trusted site -> Sites ->
Uncheck Require server verification(https:) -> enter localhost url &
click on add button.
修改以上设置后,我测试了这段代码
const popupWindow = window.open(
"https://Bing.com",
"Microsoft page",
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
Microsoft Edge 44.18362.1.0 中的输出:
在 Microsoft Edge 中,当 url
与当前域的来源不同时,以下代码片段将忽略传递给 window.open()
的选项。但它在 Chrome 和 Firefox 中工作正常,并且当域是同源时。
const popupWindow = window.open(
url,
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
我找到了一个让 window 正确调整大小的 hacky 解决方法,但这真的很烦人,你可以看到它重定向了页面。
const popupWindow = window.open(
"/#",
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
popupWindow.location.href = url;
我如何在 Microsoft Edge 上使用它?为什么 Microsoft Edge 忽略我的大小和其他参数?这是一个错误吗?
对我来说,它工作正常,所以问题可能出在您使用的 Edge 版本上。您可以使用 caniuse.com 来检查您是否可以使用东西。但是,由于您发现了这种使用它的 hacky 方式,您可以将它变成这样的函数:
function popup(url, title){
const popupWindow = window.open(
"/#",
title,
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
popupWindow.location.href = url;
}
现在您可以使用弹出功能了。
对了,我看你用的是ES6。确保您的 Edge 版本支持 const。
我尝试检查问题,它看起来像是与安全设置相关的问题。
我建议您修改以下 Internet 选项 设置。
(1) 启用 跨域访问数据源 选项。
Internet Options -> Security (Tab) -> Custom Level -> Miscellaneous -> Access data sources across domains -> Set to Enabled
(2) 禁用 保护模式.
Internet Options -> Security (Tab) -> uncheck Enable Protected mode for Internet & Local Intranet
(3) 将两个域都添加到受信任的站点列表。
Internet Options -> Security (Tab) -> Trusted sites -> Sites -> Add both domains to the list.
(4) 取消勾选需要服务器验证(https:):
Internet Options -> Security (Tab) -> Trusted site -> Sites -> Uncheck Require server verification(https:) -> enter localhost url & click on add button.
修改以上设置后,我测试了这段代码
const popupWindow = window.open(
"https://Bing.com",
"Microsoft page",
'menu=no,toolbar=no,width=700,height=600,scrollbars=1,resizable=0,' +
'directories=no,location=no,screenX=0,screenY=0,top=48,left=48',
);
Microsoft Edge 44.18362.1.0 中的输出: