Home >Web Front-end >JS Tutorial >Determine whether the object is the js code of window_javascript skills

Determine whether the object is the js code of window_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:34:141203browse

[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh 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
Look at how Heheming determines it and determine whether it has a property called setInterval. Gee, it doesn’t seem too rigorous, but it can’t be more rigorous, because it is impossible to determine whether a generalized function is a function in IE. Object.prototype.toString.call always prints [object Object], and there is no name attribute. Because it was too easy to copy, it was discarded.
In desperation, check its attributes one by one to see if there are any special attributes. I finally discovered that there is a window property with the same name, which refers to itself in an infinite loop. This is still a problem in IE.

[Ctrl A to select all Note:
If you need to introduce external Js, you need to refresh 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 necessary External Js needs to be refreshed to execute
]


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh it. Execute Copy the code


The code is as follows:

var isWindow = function(obj){
return obj.window === obj.window.window
}
=========== ===========Gorgeous dividing lines====================== Thanks to Ivony for the inspiration!
Copy code


The code is 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 is only valid for the original scope
//See http://www.javaeye.com/topic/519098 for details
//In addition, eval and with are prohibited in HTML5 strict mode, so discard them!
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] <script>alert(Object.prototype.toString.call(window))</script>} <script>alert(window === window.window)</script>isWindow(window) <script>alert(window == window.window)</script><script>alert(window.window === window.window.window)</script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn