I tried to set a variable in the very first line of my script so I could access it later(and check if it was undefined. The basic gist of the code is:
var map;
var mapIsVisible = false;
console.log(map);
function clearMap() {
if(map != undefined) {
map.clear();
}
}
clearMap();
So in FF, it works perfectly. No errors whatsoever. Chrome throws an error that says:
Uncaught TypeError: Object #<HTMLDivElement> has no method 'clear'
which led me to do that console.log after the variable instantiation and what I got was an HTMLDivElement. Why is it that FF returns undefined while Chrome says its an HTMLDivElement when it clearly isn’t? Or does chrome set that to all variables you declare that don’t have any type to them?
My fix for now was to explicitly say that map was undefined:
var map = undefined;
and my script works well. What I want to know is why this happens for Chrome
EDIT
As said in the answers, I DO have an element with an id of “map”. I didn’t know that they automatically added JS variables for that.
My guess is that you have an element on your page with a
nameoridattribute of “map.” I seem to recall id-ed elements automatically becoming global JS variables in Chrome. God knows why.