News for hackers. Stuff that matters.
Re:GetElementById vs. QuerySelector by seumars on Friday November 26, 2021 @06:00AM (#29349133)
Love this trick. It should be noted that the element is added to the window object as long as the id follows the syntax of a valid javascript variable (meaning no dashes).
Re:GetElementById vs. QuerySelector by chrismorgan on Friday November 26, 2021 @08:55AM (#29350199)
False on two counts:

1. It doesn’t need to be a valid JavaScript identifier, though if it isn’t you’ll need to use subscripting syntax to access it:

  > document.body.id = "like-this";
  > window["like-this"]
  <body id="like-this">
2. It’s not added to the window object; rather, property lookup on a window object falls back to looking up elements by ID if there is no such property:

  > typeof example
  "undefined"
  > document.body.id = "example";
  > example
  <body id="example">
  > example = "no longer an element";
  > example
  "no longer an element"
  > delete example;
  true
  > example
  <body id="example">