Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeof returning "unknown" in IE

I have a window, where before being closed I refresh the underlying page.

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

If I moved away from the original opening page, this code would throw a "Permission Denied" error.

Debugging the code revealed that typeof(opener.Refresh) was equal to "unknown" instead of the expected "undefined".

As far as I'm aware "unknown" is not one of the return values for typeof, so how and why would this value be returned?

Further Information

I avoided the error by changing the check to:

if(opener && typeof(opener.Refresh) == 'function')

However examples like this (detecting-an-undefined-object-property-in-javascript) do not seem to factor "unknown" into the equation.

like image 711
Brett Postin Avatar asked Jun 11 '12 15:06

Brett Postin


3 Answers

According to a duplicate question at Bytes, the typeof value unknown is added to JScript version 8, along with date.

A comment to a blog by Robert Nyman can also be explanatory:

Internet Explorer displays “unknown” when the object in question is on the other side of a COM+ bridge. You may not know this or realize this, but MS’s XMLHTTP object is part of a different COM+ object that implements IUnknown; when you call methods on it, you’re doing so over a COM bridge and not calling native JavaScript.

Basically that’s MS’s answer if you try to test or access something that’s not a true part of the JScript engine.

like image 176
Marcel Korpel Avatar answered Nov 13 '22 00:11

Marcel Korpel


Try in operator. I had the same problem (with applet) and I solved it using in:

if("Refresh" in opener) {
    opener.Refresh();
}
like image 31
Tomasz Dzięcielewski Avatar answered Nov 13 '22 01:11

Tomasz Dzięcielewski


The ECMAScript specification states that for host objects the return value of the typeof operator is:

Implementation-defined except may not be "undefined", "boolean", "number", or "string".

I believe the unknown value is only ever returned in Internet Explorer. Interestingly, MSDN does not mention it:

There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."

like image 25
James Allardice Avatar answered Nov 12 '22 23:11

James Allardice