最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Global variables undefined when referencing them via window object - Stack Overflow

programmeradmin3浏览0评论

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 of window.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. Using window.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 code onload, but FF will give you "good good good" if you run the code before that: jsfiddle/WHYFc/2 or if you run onload but don't use document.write(): jsfiddle/WHYFc/1 (noting that jsfiddle puts your JS code in an onload 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
 |  Show 1 more ment

2 Answers 2

Reset to default 2

Your 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);
发布评论

评论列表(0)

  1. 暂无评论