admin管理员组文章数量:1315360
Let's say I have some random code like below:
<script>
this.fn = (function() {
var element = document.createElement("div");
element.innerHTML = Object.keys(self) +
"<br />" + Object.keys(window) +
"<br />" + Object.keys(top);
self["document"].body.appendChild(element);
return arguments.callee;
})();
</script>
Not only having to do with the above code, what's the difference between:
Self
, Document
, This
, Top
, Window
?
What's a best use case for each?
Let's say I have some random code like below:
<script>
this.fn = (function() {
var element = document.createElement("div");
element.innerHTML = Object.keys(self) +
"<br />" + Object.keys(window) +
"<br />" + Object.keys(top);
self["document"].body.appendChild(element);
return arguments.callee;
})();
</script>
Not only having to do with the above code, what's the difference between:
Self
, Document
, This
, Top
, Window
?
What's a best use case for each?
1 Answer
Reset to default 10self
& window
: They both reference the current window (or frame) where the script is located and running. See here for details and examples.
document
: References the DOM container, giving you access to the headers and the body contents. See here for details and examples.
this
: References the JavaScript object under which the code is executed. JavaScript code and functions written directly inside <script>
tags have this
refer to window
. If an object's function needs to call a method in the same object, use this.method_name();
.
top
: References the top-most window
object in a frame hierarchy. If you use frames and want to manipulate the whole frameset window from inside a sub-frame, use top
, e.g. top.close();
to close the current window containing all the frames.
本文标签: javascriptSelf Document This Top WindowStack Overflow
版权声明:本文标题:javascript - Self Document This Top Window - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975609a2408103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论