Js Code used to determine whether the object is a window

Source: Internet
Author: User


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The results are varied:
[Object Object] IE6
[Object Object] IE8
[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 is an infinite loop reference itself. This is still a problem in IE.

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
IE will pop up false, and other browsers will pop up true. However, all browsers are unified as follows:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Finally, it is concluded that:
Copy codeThe Code is as follows:
Var isWindow = function (obj ){
Return obj. window = obj. window. window
}

==================================== Gorgeous demarcation line ====================== ========
Thanks for the inspiration provided by Ivony!
Copy codeThe Code is as follows:
Function isWindow (obj ){
If (typeof obj! = "Object") return false; // it must be an object
Var expando = "dom" + (new Date-0) // generate a random variable name
// Globally parse the code. The eval of IE is only valid for the original scope.
// See http://www.javaeye.com/topic/519098
// In addition, eval and with are prohibited in strict html5 mode!
Var js = document. createElement ("script ");
Var head = document. getElementsByTagName ("head") [0];
Head. insertBefore (js, head. firstChild );
Js. text = expando + "= {};"
Head. removeChild (js)
Return window [expando] === obj [expando]
}
IsWindow)

Related Article

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.