Odd javascript flashback in Chrome
I have the following in index.html:
<html><body><pre> <script src="program.js"></script> </pre></body></html> And the following in program.js:
document.writeln(JSON.stringify(name)); name = "Bob"; Opening index.html for the first time, this is done (exit A):
> "" Then, after refreshing the page, this is done (exit B):
> "Bob" I do not see exit B in Firefox.
Return to Chrome: if, however, I use some variable other than "name", for example "val":
document.writeln(JSON.stringify(val)); val = "Bob"; I get an exception:
Uncaught ReferenceError: val is not defined So, since the "name" is in global scope, I thought Chrome somehow remembers global variables from old page loads and sets the default values ββfor these variables when loading a new page. But, if I use the global variable 'status' instead, I always see output A.
Why is this happening?
It looks like the name variable in the global context is some kind of reserved name in chrome. The same applies to variables of type status , since they can only accept String values.
This name attribute refers to the name of the window object, which is preserved as long as the window remains alive, so the value is shared between several documents.
Example: var status = {}; alert(status) var status = {}; alert(status) will also warn [Object object] of the same with name .
I do not have a reference document to provide this behavior, but something that I noticed as a result in the error report in one of my projects.