Search code examples
javascriptcross-browser

Javascript check if a variable is the window


do you know a good way to check if a variable is the window object in javascript? I tried with:

var variable=window;
Object.prototype.toString.call(variable);

In Firefox it returns "[object Window]" but in IE "[object Object]" so that's not the right way. Do you know an accurate way to check it?


Solution

  • Yes but i need a way to check every window not only the current one

    There are a few ways you can do this. The simplest method is to check for one or two known properties on the window object. There's also the self property - for each window, you could check the self property is equal to the window object:

    myvar.self == myvar;
    window.self == window;
    frameElement.contentWindow.self == frameElement.contentWindow;