0

The html ids are exposed as global variables in browser.

<span id="someid" class="clsname1 clsname2 clsname3"></span>

For the above html snippet, you can find a global variable called someid. Among many things, you can do the following to it, say, in your console.

>someid.id \\gives back someid
>someid.className \\gives list of classes as string
>someid.classList \\gives an array

However, it did not conflict with existing global variables such as location, navigator, etc when you create a DOM like

1) Is it okay to access them and use in your scripts?

2) Why does browsers expose them globally like this?

3) I think developers should be cautious using id names as variables in their programs as they might impact.

Vijey
  • 6,536
  • 7
  • 43
  • 50

1 Answers1

0

Named elements are added as properties to the window object. This is called named access on the window object.

What are ‘named elements’? Anything with an id, and anything with a name being used for ‘identifying’ purposes: that is, forms, images, anchors and a few others, but not other unrelated instances of a name attribute, like control-names in form input fields, parameter names in <param> or metadata type in <meta>. ‘Identifying’ names are the ones that should should be avoided in favour of id.

You can read more about this here.

Community
  • 1
  • 1
Charlie
  • 22,886
  • 11
  • 59
  • 90