It's funny, yes, in IE? (I have 8th).

someItem = document.getElementById('someItem') 

This code gives an error. What kind of dumb programmer has banned the use of a variable that matches the element id ?? Or am I misunderstanding something?

  • Take a closer look at your code. This script will not search for someItem variable, but an element with id someItem - lampa
  • Of course, and at the same time give an error. I never checked the site code in ie And now - checked. I have the usual code. Works everywhere. Launched in ie- right there error! Changed the name of the variable - it all worked. what a garbage? - Deus
  • four
    @Deus, so what are you. Teach to blame yourself in the first place, because this is not your first question to which I answer and in almost every question you blame either the server or the interpreter, or the library developers, or the browser developers. This is not good, because the usual and correct train of thought is: " Oh, something is not working. Probably I missed something or did not follow the documentation. We need to look at everything in more detail. " And along the way, you will still find out a huge amount of new information :) And your questions are completely wrong: ( - - KryDos
  • one
    The question was not whether it is possible to declare variables without the var instruction, and whether this will result in the rewriting of global variables, but that ie, ALLOW this use of variables, but - fact - if their names coincide with id-values, FATAL error. An exception. Stop the program.! - Deus
  • one
    Why did you decide to create a variable in the global domain? I can know 3-4 cases when it is necessary, from the real world it would be like if you decided to post an advertisement for the sale of a Soviet washing machine to a brass rider. - zb '

1 answer 1

What you call a global variable is not. The execution of the script goes in the context of the window (the window object). Assigning (without var) x = 0 creates a property on the window object with the name x. In IE (and for some time in other browsers) all elements of the document with the specified id are available as "global" variables, i.e. as properties of the window object. The fact that the error occurs only in IE8, and, for example, in IE9, your assignment works as you would expect - this is only an implementation in a specific browser version. In version 8, when you try to overwrite a property that points to a DOM element, an exception is thrown; the setter is not implemented just apparently.

Try this code:

 alert = 0; 

Yes, IE8 will produce the same error. In IE9 and other alert browsers it will no longer work and will cease as expected to be a function.

Threat Why use these "global" variables is bad, I think, understandable.
ZZY But the IE developers do not have to kick, they have already been cursed by several generations of web developers, there is no difference from another curse.

  • Thanks for the intelligible answer. And I called a “global variable” a variable that is global relative to the windows object, i.e. cannot be overridden in a function with a var statement. It is surprising that in such a serious matter as namespace browsers are so ambiguous. - Deus
  • @eicto, in general, I once had a code of this kind that in the html file in the body tag there was only a declaration of empty divs with their ah-di, and the rest was done by java script code. The code declared global variables that contain references to home-objects, so that the functions make it easier to access them. And what not? - Deus
  • global, I have already explained why, besides, as you now know, <div id="mydiv"> generates the variable window.mydiv, which without a var declaration is global in your particular case, this assignment doesn’t need someItem without it refers to your item. - zb '
  • 3
    There is such a thing, best practice. For this case, always use var, always use a semicolon, etc. Following the "good practice" removes a lot of questions right away. And to find out about it and about the behavior of variables need, oddly enough, from the documentation and textbooks. About the use of global variables - also from books, about programming :) Convince that the sky is blue and the grass is green for a person who does not have color vision, I do not see much sense, about this and so tons of words are written in public sources .. - user6550
  • 2
    @Deus in js anywhere but firefox , there are no constants - zb '22