Determines whether the object is a window.

Source: Internet
Author: User

This is a very interesting question. Let's start with object. Prototype. tostring to see if it can be solved.

<Br/> alert (object. Prototype. tostring. Call (window) <br/>

RunCode

The results are varied:

    • [Object object] IE6
    • [Object object] IE8
    • [Object window] ie9
    • [Object window] firefox3.6
    • [Object window] opera10
    • [Object domwindow] safai4.04
    • [Object Global] chrome5.0.3.22

Let's take a look at how to determine whether a famous person has a property called setinterval. It seems that the generalization function is not rigorous, but it cannot be rigorous, because in IE, it cannot determine whether the generalized function is a function, object. prototype. tostring. [Object] is displayed for all calls, and there is no name attribute. Because it is too easy to copy, discard it.

In desperation, check the attributes one by one to see what special attributes are available. Finally, we found that there is a window attribute with the same name, which references itself infinitely. This is still a problem in IE.

<Br/> alert (window === window. Window) <br/>

Run code

IE will pop up false, and other browsers will pop up true. However, all browsers are unified as follows:

<Br/> alert (window = Window. Window) <br/>

Run code

Are there any rigorous judgments? Do not forget that window references itself in an infinite loop. It should be said that it references the previous one. Therefore, we can:

<Br/> alert (window. Window === window. Window. Window) <br/>

Run code

Finally, it is concluded that:

VaR iswindow = function (OBJ) {return obj. Window = obj. Window. Window}

==================================== Gorgeous demarcation line ====================== ========

Thanks for the inspiration provided by ivony!

<Br/> var Dom ={}; <br/> Dom. iswindow = function (OBJ) {<br/> If (! OBJ |! OBJ. Window |! Obj.doc ument) <br/> return false; <br/> var expando = "dom" + (new date-0) // generate a random variable name <br/> var Doc = obj.doc ument; <br/> // The Global parsing code, the eval of IE is only valid for the original scope <br/> // For details, see the http://www.javaeye.com/topic/519098 <br/> // In addition, Eval and with are things to be prohibited under the strict HTML5 mode, discard it! <Br/> try {<br/> var JS = Doc. createelement ("script"); <br/> var head = Doc. getelementsbytagname ("head") [0]; <br/> head. insertbefore (JS, head. firstchild); <br/> Js. TEXT = expando + "={};" <br/> head. removechild (JS); <br/> var ret = (Doc. parentwindow | Doc. defaultview) [expando] === OBJ [expando]; <br/> OBJ [expando] = void 0; <br/>} catch (E) {<br/> return false; <br/>}< br/> return ret; <br/>}< br/> var test1 = {}; <br/> test1.window = test1; <br/> test1.document = Document; <br/> alert (Dom. iswindow (test1) </P> <p> var Test2 ={}; <br/> test2.window = Window; <br/> test2.document = document; <br/> alert (Dom. iswindow (Test2) </P> <p> alert (Dom. iswindow (window) <br/>

Run code

2011.10.4 update

</P> <p> var Dom ={}, <br/> windowstring = {<br/> "[Object window]": 1, <br/> "[object domwindow]": 1, <br/> "[object Global]": 1 <br/> }; <br/> // by situ zhengmei http://www.cnblogs.com/rubylouvre/ <br/> Dom. iswindow = function (OBJ) {<br/> If (! OBJ | typeof OBJ! = "Object") // must be an object <br/> return false; <br/> If (windowstring [windowstring. tostring. call (OBJ)]) <br/> return true; <br/> return OBJ = obj.doc ument & obj.doc ument! = OBJ <br/>}< br/> // test code <br/> var test1 ={}; <br/> test1.window = test1; <br/> test1.document = Document; <br/> alert (Dom. iswindow (test1) // false </P> <p> var Test2 ={}; <br/> test2.window = Window; <br/> test2.document = document; <br/> alert (Dom. iswindow (Test2) // false </P> <p> alert (Dom. iswindow (window) // true <br/> var IFRAME = document. createelement ("iframe"); <br/> document. body. appendchild (IFRAME); <br/> var iwin = IFRAME. contentWindow | IFRAME. contentdocument. parentwindow; <br/> alert (Dom. iswindow (iwin); // true <br/> document. body. removechild (IFRAME); </P> <p> var WG = {document :{}}, wgdoc = wg.doc ument; <br/> WG. window = WG; <br/> wgdoc. createelement = function () {return WG ;}; <br/> wgdoc. getelementsbytagname = function () {return [WG] ;}; <br/> wgdoc. parentwindow = WG; <br/> WG. insertbefore = function () {}; <br/> WG. firstchild = WG. firstchild; <br/> WG. removechild = function () {}; <br/> alert (Dom. iswindow (WG); // false </P> <p>

Run code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.