For a start - a little theory. This is IMHO stupidity, at one time, started IE, this was not supported by other browsers, after - it became supported by chrome, at the same time - this kind of behavior is approved by the html5 standard (yes, and there is something through F, well, not the essence ). Accordingly, support is really at the level of support (except for the paradox that it will not work in old non- IE), moreover, support will remain and it will not go anywhere, so I don’t quite understand what is right in the answer @ Alex Silaev ?
Well, now let's get to the practical part, such elements are stored in the global scope, respectively, for example, a situation may arise, such as:
<!-- наш HTML --> <div id="hello">классно работает!11!</div> <!-- один из наших скриптов --> var hello = function(){ /* function code... */ } <!-- другой наш скрипт --> hello.onclick = function(){ alert(this.innerHTML); }
What happens in this case, I think it is clear Naturally, someone may argue, they say - if you do everything neatly, then everything will be ok and I do not deny it, often it will be so. But, to put it mildly - not always. The more code you have - the higher the probability of catching an unpleasant bug will become. Of course, if you understand that global variables are evil, etc. etc. - you will correctly structure your code, but in this case you will also choose the elements, humanely. Otherwise, you have a hard binding to the html structure, even worse than with document.getElementById ('someId'). Actually - all this looks like magic, respectively, looking through the code, trite, it is not clear - where did this hello come from, for example, it becomes obvious when the amount of code becomes, at least 300+ lines. Suppose I need to change this id, I change it in DOM, I search by the scripts where it is used, but I still don’t find it. (you can be sure) - I’ll look for exactly $
in the case of jq, prototype and getElementById in the case of "pure" JS, now I’m not writing in the context of myself, but in the context of the person who will look at the code you wrote, your partner or who replaces you at work. And, in fact, this is normal, not everyone knows this, and even in books where it is mentioned - they write briefly, they say, look at it this way you can do it, describe how it works and then it is possible , but not necessary In all books, something like this is written further - accordingly, in our book we will not do that. Of course, I understand that everything is not always written correctly in books (this is especially true with books on javascript), but the fact that not all books are written about this and that, for example, you haven’t I knew this, in my opinion, already says that no one needs it. offtopic - and confirms the saying "you know less - sleep better" =)
I would also like to add, to the above, the inconvenience in terms of pairing with the coder or, in this case, much worse - with another JS developer.
In general, briefly summarizing - to put it mildly - I do not recommend (not just me) in the absolute majority of cases, the reasons (without inventing something complicated, but in real life, believe me - the problems will be more interesting) are described above. What do we win? Yes, actually, we gain practically nothing ...
One more “type” argument can be that it is faster — perhaps it is so, but getElementById works so quickly (and absolutely everywhere) that it is not serious to be guided by this. But even if you want, do something like this:
function getElementById(id) { return window[id] !== undefined && window[id].id === id ? window[id] : document.getElementById(id); }
Actually, such an approach solves the problem of "erasing", but, at the same time, it does not relieve the brevity of the record. On the other hand, this function will work normally everywhere and in the DOM; if the element is saved in the window, it will not work. Once again I draw your attention - this is saving on matches.
I parry a piece of UPD by @ Anton Mukhin about the id of the form "div-id" - you can easily and easily get them by calling window ["div-id"], the browser does not calculate anything.
I will try to sum up very brief results - just don’t use it, know that you can understand how this or that javascript code works, no more (IMHO)
PS: 2 @northerner nobody says that they are fools there, they want to do better. Regarding javascript without getElementById - if the question explicitly asks, “how is getElementById better or so?”, What is the relation to the question that you wrote about? Either I misunderstand something, or you did not carefully read the question.
UPD: Time has already shown everything, if it were needed, it would have been used in wide circles a long time ago, because nothing like that is observed - nobody needs it (or rather, someone needs to adopt such standards once, but to whom - I have no idea). I will once again emphasize that we are talking about browser javascript, perhaps somewhere, this is convenient and is practiced by the majority, then of course - use your health (thanks to cap).