2

I've been baffled by this, dom ID names are defined as dom objects in js by default. I was going mad because of a variable I thought I defined but I actually did not, and it was returning an object because of same ID name.

I was not able to get any documentation about this. All browsers seems to be doing that. Is there any reason for this?

id name defined as dom object by default

eringen
  • 381
  • 1
  • 7
  • 3
    Since Internet Explorer added support for JavaScript in the 1990s. – Pointy Oct 12 '16 at 13:37
  • 2
    Since forever in Internet Explorer. Since HTML5 for the other browsers. This behavior is now standardized and unavoidable. – Frédéric Hamidi Oct 12 '16 at 13:37
  • I do not think it was like that since IE, I clearly remember cost of forgetting getElementById() and I was always using same variable name with ID name. Even I'm wrong, there should be some sort of documentation about this behavior. Or people should be saying "f the get ElementById, just use id name!" – eringen Oct 12 '16 at 13:47
  • @eringen No, you should not do that, see [Why don't we just use element IDs as identifiers in JavaScript?](https://stackoverflow.com/q/25325221/1048572) – Bergi Oct 12 '16 at 14:05
  • @Bergi I completely agree and share exactly the same thoughts, my gripe is how did I miss this until now, and how come there is no documentation about this bs. – eringen Oct 12 '16 at 14:20
  • bleeding global variables via an api is just plain stupid. anyways. – eringen Oct 12 '16 at 14:22
  • Yes, it's stupid, but it is what Microsoft did in the early days and then other browsers had to support the "feature" to be compatible. – Bergi Oct 12 '16 at 14:42
  • 1
    @eringen: IE had this "feature" since the first public release with Javascript support (IE3) in 1996. So yes, IE had it since effectively "forever". Forgetting `getElementById` used to cause your page to break on Netscape. It's one of the things that caused the IE-only corporate web portals to exist - because IE developers tend to use such shortcuts but their webpages won't work on other browsers. – slebetman Oct 12 '16 at 16:05

0 Answers0