admin管理员组文章数量:1327661
I'm fairly new to declaring variables globally via window, so I was a bit surprised that the following snippet behaves differently depending on the browser.
window.test = "Good";
document.write(window.test);
document.write('<br>');
document.write(window.test);
document.write('<br>');
document.write(test);
Firefox, IE, Opera
Good
undefined
Good
Chrome and Safari
Good
Good
Good
My initial belief was that it should behave the way Chrome and Safari does, but I realize I might not have a proper understanding of the window object, so would anyone more knowledgeable explain this?
I realize I can just use var test = "Good";
for that scope, but I'm interested in why the browsers handle it differently.
/
I'm fairly new to declaring variables globally via window, so I was a bit surprised that the following snippet behaves differently depending on the browser.
window.test = "Good";
document.write(window.test);
document.write('<br>');
document.write(window.test);
document.write('<br>');
document.write(test);
Firefox, IE, Opera
Good
undefined
Good
Chrome and Safari
Good
Good
Good
My initial belief was that it should behave the way Chrome and Safari does, but I realize I might not have a proper understanding of the window object, so would anyone more knowledgeable explain this?
I realize I can just use var test = "Good";
for that scope, but I'm interested in why the browsers handle it differently.
http://jsfiddle/WHYFc/
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 19, 2012 at 5:04 Michael TheriotMichael Theriot 1,0221 gold badge8 silver badges13 bronze badges 6-
Something else to test is using
window["test"]
in place ofwindow.test
, but I'm sure those results will be the same. – Ian Commented Oct 19, 2012 at 5:06 -
What if you use
window.test
the third time? – Alvin Wong Commented Oct 19, 2012 at 5:07 -
I did test
window["test"]
but the results were the same. Usingwindow.test
any time other than the first returns undefined. – Michael Theriot Commented Oct 19, 2012 at 5:09 - I'm fairly new to declaring variables globally via window -> you'd better keep it that way. yuiblog./blog/2006/06/01/global-domination – KooiInc Commented Oct 19, 2012 at 5:10
-
It's because you're using
document.write()
after the document has loaded. Your fiddle is set to run the codeonload
, but FF will give you "good good good" if you run the code before that: jsfiddle/WHYFc/2 or if you runonload
but don't usedocument.write()
: jsfiddle/WHYFc/1 (noting that jsfiddle puts your JS code in anonload
handler by default; you can change this via the drop-down on the left).document.write()
is almost always the wrong choice... – nnnnnn Commented Oct 19, 2012 at 5:15
2 Answers
Reset to default 2Your JSFiddle is using the window load event to create the script.
document.write after load CLEARS/WIPES the document so what you are seeing is normal for those browsers and webkit simply is more lenient
Here is the code as it might look in jsfiddle:
window.addEventListener('load', function() {
window.test = "Good";
document.write(window.test);
document.write('<br>');
document.write(window.test);
document.write('<br>');
document.write(test);
});
Change your fiddle to head or body and it will work as expected
If you want to store something globally even though you're opening new documents, you could (kind of ironically) use the document
object, which seems to be persistant.
document.test = "Good";
document.write(document.test); // > "Good"
document.write('<br>');
document.write(document.test); // > "Good"
document.close();
setTimeout(() => {
document.write("Also " + document.test); // > "Also Good"
document.close();
}, 2000);
本文标签: javascriptGlobal variables undefined when referencing them via window objectStack Overflow
版权声明:本文标题:javascript - Global variables undefined when referencing them via window object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227334a2436568.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论