Reputation: 13054
I came across this gist today, and in the comments the author mentioned that
var log = document.getElementById('log');
is unnecessary, because in Javascript you can just use log
to access the DOM element. Is this true across all browsers? Is there a name/reference for this technique?
Upvotes: 6
Views: 584
Reputation: 6146
Surprisingly this is actually in the HTML5 spec, so yes it will work and no it is not a legacy feature as it is being suggested (anymore at least - it used to be an IE only trick).
You can read the spec here, http://www.whatwg.org/specs/web-apps/current-work/#named-access-on-the-window-object
I will agree that it's actually clutters the namespace, and would take the liberty to suggest against using it - but it will and does work on every major browser!
Upvotes: 4
Reputation: 23863
Under Internet Explorer where IDs will become global variables and accessible everywhere by that name.
I regard this 'misfeature' as a bad thing.
I don't recall all of the features and aspects of it and I do my best to try and pretend it never happened.
In short: IE-only "feature" -- best ignored. Read this article for more details.
Upvotes: 4
Reputation: 11132
This is a dubious "feature" that shouldn't be used for anything. It introduces global state and globally scoped variables which are one of the main points to avoid in any application and more importantly seems only to work in Internet Explorer and some versions of Chrome. It is almost certain to be less performant, since not only does the browser have to access the DOM but it also must look for a variable named log
beforehand.
Upvotes: 1
Reputation: 2785
For new versions of modern browsers it might work, but older version will not handle it fine. If you want your site works in older browser, do it using document.getElementById.
Upvotes: 0