开发者

Checking if a function is defined in the opener window in IE8

I have a pop-up that allows the opener window to optionally define a callback function, which if defined will be called when the user is done with the pop-up. Based on the advice I've read I'm doing this:

if (window.opener && (typeof window.opener.callbackFunction == 'function')) {
  window.opener.callbackFunction()
}

This works fine in Firefox - when the function is defined, the typeof is "function" as intended. However, in IE8 the typeof is "object" instead. The function is defined normally in the opener, like so:

function callbackFunction() {
  ...
}

Does anybody know why开发者_高级运维 the typeof would be different in IE8? I'm also open to other suggestions as to how to accomplish this. I also tried if (window.opener && window.opener.callbackFunction) but that caused IE8 to blow up when the function wasn't defined.


You can try

if ( window.opener && (typeof window.opener.callbackFunction != 'undefined') {
  window.opener.callbackFunction();
}

I don't have IE currently so I can not test this but believe it will work.


It's a hack, but this will work:

if (typeof window.opener.callbackFunction == 'object') {
   // this first 'if' is required because window.opener returns an object even
   // if window.opener has been closed
   if(window.opener.callbackFunction.toString().substr(0,8) == 'function') {
      window.opener.callbackFunction();
   }
}

Note: It will fail for some native browser functions like alert().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜