To determine whether the object is window JS code _javascript Tips

Source: Internet
Author: User
<script>alert (Object.prototype.toString.call (window)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The result is a multitude of:
[Object] IE6
[Object] IE8
[Object window]firefox3.6
[Object window]opera10
[Object domwindow]safai4.04
[Object global]chrome5.0.3.22
Look at the famous how to determine, it is determined whether there is a property called SetInterval. Gee, it seems not too rigorous, but also can not be rigorous, because in IE can not determine whether the function of the generalization function, Object.prototype.toString.call all played [object], there is no Name attribute. Because it is too easy to imitate, discard it.
Helpless, check its properties, see what special attributes. Finally found that there is a window property with the same name, it is an infinite loop referencing itself. This is a bit more problematic in IE.
<script>alert (window = = Window.window) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

IE will eject false and other browsers may eject true. But here's how all browsers are unified:
<script>alert (window = = Window.window) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Is there a rigorous decision? Do not forget that window is an infinite loop referencing itself, it should be said to refer to the previous self. Therefore, we can:
<script>alert (Window.window = = Window.window.window) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The final conclusion:
Copy Code code as follows:

var IsWindow = function (obj) {
return Obj.window = = Obj.window.window
}

====================== Gorgeous line of demarcation =====================
Thank Ivony for the inspiration!
Copy Code code as follows:

function IsWindow (obj) {
if (typeof obj!== "Object") return false;//must be An object
var expando = "dom" + (new Date-0)//Generate a random variable name
//Global parsing code, IE eval only valid for the original scope
//See http://www.javaeye.com/to pic/519098
//plus eval and with is the HTML5 strict mode to prohibit things, discard it!
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 (window)

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.