8/24/2023 0 Comments Set name of element![]() But there are some things you might want to consider as you do. Let’s say we chuck the criticisms against using named globals and use them anyway. A couple of folks ran tests in this StackOverflow thread, where named globals were actually more performant in one test and less performant in a more recent test. ![]() Maybe there’s a performance cost? I mean, the browser’s gotta make that list of references and maintain it.But doing so won’t stop a page from working or anything like that. Then again, the spec says we ought to use one instance of an id in an element’s tree anyway. Firefox, however, only returns the first instance. According to the spec, when there are multiple instances of the same named element in the DOM - say, two instances of - the browser should return an HTMLCollection with an array of the instances. but some browsers (namely Safari and Firefox) return a ReferenceError in the console. For example, we should be able to access an anchor with an id - e.g. It is implemented differently in browsers.A simple typo may very well wind up referencing a named global and give you unexpected results. Accidental references are far too easy.That makes for some really “brittle” ( the spec’s term for it) code where the separation of concerns between HTML and JavaScript might be too much. If the DOM changes, then so does the reference.There are lots of reasons for that, which TJ VanToll has covered nicely over at his blog and I will summarize here: The case against named globalsĮarlier, I said that using global named elements as references might not be the greatest idea. This safeguarding technique may very well be the why you - if you’re like me - are learning about this for the first time. This behavior is essential because it nullifies dangerous overrides such as, which would otherwise create a conflict by invalidating the alert API. For example: Īnd the opposite is true as well: I will be overridden :( So, if a DOM element has an id that is already defined as a global, it won’t override the existing one. Probably the most interesting part of this feature is that named element references don’t shadow existing global variables. One thing that might not be well known is that browsers had to put in place a few precautionary measures - with varying degrees of success - to ensure generated globals don’t break the webpage. There was hesitation to implement it at all, but it moved ahead in the name of browser compatibility (Gecko even tried to convince WebKit to move it out of standards mode) and eventually made it to standards mode in Firefox 14. Gecko was the only browser at the time to not support it directly in standards mode, opting instead to make it an experimental feature. Internet Explorer was the first to implement the feature. ![]() This approach is outlined in the HTML specification, where it’s described as “named access on the Window object.” Some folks have come to call this the “global scope polluter.” We’ll get into why that is, but first… Some context ![]() Again, this isn’t exactly “new” but it’s really uncommon to see.Īs you may guess, accessing the global scope with named references isn’t the greatest idea. So, any id - or name attribute, for that matter - in the HTML can be accessed in JavaScript using window.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |