A picture of me taking it easy

I'm currently on parental leave till September. During that time, I will not read any e-mail or blog comments.

Until I'm back, please read through my archives, take a look at my code/applications and check out my pictures.

Have a great summer, and a splendid winter to you aussies and kiwis! :-)

What is typeof “unknown”?

Published on Wednesday, December 21st, 2005

Yesterday when I was testing an AJAX script in an application I’m working on, I wanted to use the abort() method of the XmlHttpRequest object to cancel current outstanding data transfers (if this is all Greek to you, don’t worry, one day I will write more about AJAX).

IE unexpectedly threw errors when I was trying to use that method, and it did as well when I tried to use object detection to check for it. Weird. So I resorted to use typeof to check what it returned, and to my amazement it returned “unknown”! To my knowledge, the only valid and possible values in JavaScript to get when using the typeof operator are these:

  • number
  • string
  • boolean
  • object
  • function
  • undefined

So is this is an IE bug? Or some freaky ActiveXObject hocus pocus? Please let me know if you have any idea!

Code to test with:


// Note! This code will only work in Internet Explorer
if(typeof window.ActiveXObject != "undefined"){
	oXMLHTTPRequest = new ActiveXObject("Msxml2.XMLHTTP");
	alert(typeof oXMLHTTPRequest.abort);
}
Posted in JavaScript | 12 comments

12 comments

  • Mats
    December 21st, 2005 at 11:24

    Maybe it’s related to the “not implemented yet” type … ;)

    I don’t remember when I ran across it but I remember seeing it on one occasion.

  • Rowan Lewis
    December 21st, 2005 at 12:04

    I wish I could help, but I can’t :/

    The reason I’m posting this is the ad under the article, it says:

    “INSTANT DOWNLOAD ! - Download right now!”

    Perhaps you should change ad provider…

  • Jens Wedin
    December 21st, 2005 at 13:54

    I know this does not have anyting to do with the article but I clicked the download now link. haha, what a joke, nice stealing from mozilla.org

  • Peter Leing
    December 21st, 2005 at 14:47

    Peter-Paul Koch had done some work with aborting XMLHTTP requests back in September. Maybe it will help you.

    XMLHTTP notes: abort() and Mozilla bug

  • Robert Nyman - author
    December 21st, 2005 at 16:13

    Mats,

    Yes, it sounds like a “not implemented” thing, I’ve seen that one too.

    Rowan, Jens,

    Yeah, maybe I should change ad provider… :-)
    And yes, that web site looks pretty darn similar to the Mozilla web site.

    Peter,

    Yes, I’ve read that article, which is very good. And I guess his conclusion is right, that it’s only necessary to abort calls in Mozilla.

    Anyway, it doesn’t explain Microsoft’s own interpretation of what typeof can return… :-)

  • Peter Leing
    December 21st, 2005 at 16:49

    Unless it is like Mats says, it may be a bug then. Microsoft’s MSDN docs on Jscript only list the 6 typeof values you state.

    JScript .NET typeof Operator

    JScript typeof Operator (JScript 5.6)

    I also tried the type of .open and .send and got the same ‘undefined’ message back. It may just be that trying to get the typeof from an ActiveXObject’s Method is not supported or working.

  • Johan
    December 22nd, 2005 at 4:48

    Maybe it is a syntax error

    typeof window.ActiveXObject != “undefined”

    rewrite as (boolean operator):

    typeof window.ActiveXObject !== undefined

  • Robert Nyman - author
    December 22nd, 2005 at 9:00

    Peter,

    Yes, maybe just a bug. However, the open() and send() methods do work, even though they also return typeof “unknown”, as opposed to the abort() method that throws an error.

    Weird…

    Johan,

    Thanks for the idea, although the line that goes wrong is when I want to check for support for the abort() method.

  • Vojtech
    June 13th, 2006 at 12:15

    I met the “unknown” type when debugging one script working with an X3D Player. I had to write something like this:

    function test(value)
    {
    if ((typeof value == "object") && (value == null)) {value = "null";}
    if (typeof value == "undefined") {value = "really_undefined";}
    if ((typeof value.toString == "undefined") || (typeof value.toString == "unknown")) {value = "no_toString";}
    return value + ":";
    }

    Without the third “if” I got “undefined” (no colon).

  • sudhakar
    July 12th, 2006 at 23:24

    I also found that IE returns ‘unknown’ when using ‘typeof’.

    It seems to happen in the following case:

    Add HTML form elements (such as textboxes, drop down lists etc.) to a container (such as a div) dynamically using javascript and then reset container’s innerHTML to empty (div.innerHTML = ”).

    Repeat this again, this time selecting enabling/disabling a few of the HTML form elements.

    Next when you again add form elements to the DIV and try to access these elements by javascript, IE returns ‘unknown’ when using ‘typeof’.

    I believe this has got some thing to do clearing these elements from IE’s DOM. Apparently setting innerHTML = ” does not seem do this as IE could not figure out using ‘typeof’.

  • Tom Trenka
    August 31st, 2006 at 15:59

    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.

    Before you complain about it further, bear in mind that MSXML was designed to be used from any MS platform, including VB and C++; there are methods and properties on XMLHTTP that are useless in scripting and even cause some confusion (such as the much maligned onreadystate, which makes plenty of sense once you realize that MSXMLHTTP, when used from C++ and VB, give you full interactive access to the internal IStream via the responseStream property.)

  • Robert Nyman - author
    August 31st, 2006 at 20:07

    Vojtech,

    Thanks for sharing!

    sudhakar,

    Interesting!

    Tom Trenka,

    Very interesting reading. However, it seems that that can’t be applied to everything, given sudhakar’s example above.

Share your thoughts:

HTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> . If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Comment preview

All the proceeds from ad clicks will go to charity. However, if you like to give something directly to charity yourself, I recommend choosing from the listed ones below.

  • Save the Children
  • Red Cross
  • Cancer Research UK
  • WWF

Top results