1

I tested the following code in FF 3.6 and IE 8. The former alerts true, the latter alerts false. Why is this and how can I accomplish the same behavior in both browsers? Thank you!

<!DOCTYPE html>
<html>
    <head>
        <script>
            alert(window.location instanceof Location);
        </script>
    </head>
    <body>
    </body>
</html>
Philippe Gerber
  • 17,457
  • 6
  • 45
  • 40
  • 2
    Perhaps you should explain what is the purpose of this check? I don't think such internal class names (Location, HTMLElement, etc.) are shared completely across browsers, so it might be helpful to know what you're trying to do to suggest alternatives. – Jani Hartikainen Jan 07 '11 at 21:15
  • Interesting observation: getting the Constructor name of the location object in IE also fails. Using the technique in http://stackoverflow.com/questions/332422/how-do-i-get-the-name-of-an-objects-type-in-javascript – kander Jan 07 '11 at 21:20
  • I'm working on a jQuery plugin which maps URL anchors to JS functions. The plugin takes an argument which either can be a DOM element or a `Location` object. So I need a way to check what kind a given argument is. :) – Philippe Gerber Jan 07 '11 at 21:42
  • 1
    I updated my answer. You'll be better off testing for the `nodeType` property that will be present if it was an DOM element. Then you could also test for some of the properties of the `location` object if desired. Of course someone could pass in some other object that has those properties, so it isn't absolutely perfect. But if someone is using your plugin, it wouldn't seem likely that they would try to defeat the test like that. – user113716 Jan 07 '11 at 21:48

2 Answers2

1

EDIT: With regard to the comment under your question, it sounds like you need to test for the difference between a DOM element, and a Location object.

You'll probably be better off testing for the DOM element using the nodeType property.

if( obj.nodeType ) {
    // it was a DOM element
}

Then you can test for various properties if nodeType wasn't found, just to be certain it is a location object.

if( obj.nodeType ) {
    // it was a DOM element
} else if( obj.hostname && obj.pathname ) {
    // it was a location object
} else {
    // some ERROR
}

Original answer:

This works for me in Chrome, Safari, Firefox:

Object.prototype.toString.call(window.location) == '[object Location]';

I can't test in IE right this minute, though.

This is the method noted in the ECMAScript spec Section 8.6.2 for getting the internal [[Class]] property name.

user113716
  • 318,772
  • 63
  • 451
  • 440
  • Returns `[object Object]` in IE. :( – Philippe Gerber Jan 07 '11 at 21:38
  • @Philippe: Yes, just got done testing it too. If this is needed, you may need to do a couple of separate tests including [the answer](http://stackoverflow.com/questions/4630313/why-is-window-location-instanceof-location-false-in-ie/4630355#4630355) from [@Phrogz](http://stackoverflow.com/users/405017/phrogz). EDIT: Although that one seems to fail in IE7. – user113716 Jan 07 '11 at 21:40
0
location.constructor == Location

Works in IE and Firefox; does not work in Chrome. (There is no Location object in Chrome.) Would only work if you are testing the location from the same window as your script is running in, however.

What are you really trying to do?

Phrogz
  • 296,393
  • 112
  • 651
  • 745