0

eg. there is a form

<form name="formA" action="https://www.myhomept.co/z_jay/test/php/requestTest.php" method="post">
  <input name="inputA" value="inputValueA">
  <button type="submit">submit</button>
</form>

I use console.log("window.formA", window.formA); and I can have return, but I can't find window.formA from console.log(window).

Weijing Lin
  • 575
  • 2
  • 5
  • 16

1 Answers1

2

In the early days of the web (before standards), IE decided that all named and id'd elements should become implicit global variables. Other browsers followed suit.

Since the window is the global object, some browsers decided to attach these implicit globals to that. But, because of the danger of an implicit global name conflicting with explicit properties of window, some user agents decided to attach them to the document object (where this danger still exists).

When you log window however, you won't see those implicit properties simply because the browser is keeping them separate from explicit properties.

So, here we are today (20+ years later) and for legacy reasons, you can still refer to a named (or id'd) element just by using its name - no DOM searching. But, because this behavior was never formally standardized and because of the very issue that you bring up with your question, there is confusion about how to use them and why you get a "now you see me, now you don't" behavior.

The moral of the story is, follow standards and stay away from globals, they do more harm than good.

// Implicit global 
var g = "global";

console.log(window.g);  // "global"


console.log(window);  // "g" will be listed
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71