I was using the Developer Tools in Chrome 13 when I typed this line:
var location = "Hello";
Upon pressing enter, the page changed and gave me a 404 error. The address bar now had Hello appended to the last address.
I swear that I have typed the exact same lines into Chrome in the past and not had the same problem. I thought location was at window.location.
Has something changed, or have I just never noticed this before?
It’s perfectly normal that the context in the developer tools would be
window. Typethisand see what is says. It’s probablywindow.Thus, when you type:
You are trying to redefine a variable in the global scope that already exists. The global scope in a browser is the
windowobject. Thus,locationin the global scope is the same aswindow.location.Trying to redefine an object that already exists (by using
var) is not an error in javascript. It just ignores thevardeclaration and does an assignment. And, assigning a string to the location object, goes to a new web page.