检查window.open支持 - JavaScript

时间:2022-10-22 15:30:43

I am working on fixing a bug regarding window.open being triggered on mobile safari. Whenever this happens, an error is triggered as it seems window is not defined or the function is not supported?

我正在修复一个关于window.open在移动safari上触发的bug。每当发生这种情况时,会触发错误,因为它似乎没有定义窗口或者不支持该功能?

The approach I am considering taking is to check if window.open is supported. If so, open the window like normal. If not, simply redirect the page. But how could I check for this support?

我正在考虑的方法是检查window.open是否受支持。如果是这样,请像平常一样打开窗户。如果没有,只需重定向页面即可。但我怎么能检查这个支持?

if (window.open) {
   var helpWindow = window.open('help.htm', 'DBHelp', 'width=800,height=600,left=100,top=100');
} else {
   this.navigateTo('help.htm');
}

This is what I am trying, only with some dummy data in the URL for now. How could I best go about this?

这就是我正在尝试的,目前只有URL中的一些虚拟数据。我怎么能最好地解决这个问题?

2 个解决方案

#1


0  

Use window.open to open a random page and check for the body element. If it's there, then the page opened correctly, so window.open is supported.

使用window.open打开随机页面并检查body元素。如果它在那里,那么页面打开正确,所以支持window.open。

Edit: Even better, window.open will return null if the call failed.

编辑:更好的是,如果调用失败,window.open将返回null。

#2


0  

It sounds like window.open is defined, but it's not able to open a new window. So check the return value rather than the function name.

听起来像window.open已定义,但它无法打开新窗口。因此,请检查返回值而不是函数名称。

var helpWindow = window.open('help.htm', 'DBHelp', 'width=800,height=600,left=100,top=100');
if (!helpWindow) {
    this.navigateTo('help.html');
}

#1


0  

Use window.open to open a random page and check for the body element. If it's there, then the page opened correctly, so window.open is supported.

使用window.open打开随机页面并检查body元素。如果它在那里,那么页面打开正确,所以支持window.open。

Edit: Even better, window.open will return null if the call failed.

编辑:更好的是,如果调用失败,window.open将返回null。

#2


0  

It sounds like window.open is defined, but it's not able to open a new window. So check the return value rather than the function name.

听起来像window.open已定义,但它无法打开新窗口。因此,请检查返回值而不是函数名称。

var helpWindow = window.open('help.htm', 'DBHelp', 'width=800,height=600,left=100,top=100');
if (!helpWindow) {
    this.navigateTo('help.html');
}