Unsafe Names for HTML Form Controls

Extra Properties: global

Extra Properties: The Element Id Resolver Object

IE, Opera, Firefox, and Webkit seem to implement a non-standard element id resolver object. This object is used to retrieve the element. See comp.lang.javscript thread: Why "window.alert()" over "alert()"?, comment by Lasse Reichstein Nielsen.

In some browsers, this behavior depends on the DOCTYPE used.

This is a non-standard behavior of browsers that applies to any element with an id, not only form controls.

Assigning to an undeclared identifier should result in a new global property being created, or, if the global/window object has a property with that name, its value should be replaced. However in some browsers, the assigment can result in an error.

The properties of the "element resolver" may appear to be shadowed by properties of the global object, or vice-versa.

Property Assignment to Element

The following example should result: undefined, 11.

 
try {
  var b1;
  globalVal = [String(b1)]; // Should be "undefined".
  b1 = 11;
  globalVal = globalVal.concat(b1); // Should be ["undefined", 11]
} catch(ex) {
  globalVal = ex.message;
}

The following example does not declare a variable. The browser may optionally resolve the identifier b2 on the element id resolver object.

 
try {
  globalVal = [typeof b2];
  b2 = 11; // undeclared assignment.
  globalVal = globalVal.concat(b2);
} catch(ex) {
  globalVal = ex.message;
}

The following example should result: undefined, 11.

 
// remove element id.
document.getElementById('b3').id = '';

try {
  globalVal = [typeof b3]; // should be "undefined".
  b3 = 11; // undeclared assignment.
  globalVal = globalVal.concat(b3);
} catch(ex) {
  globalVal = ex.message;
}

Declared variables that have not yet been assigned a value create a property on the Variable object with the value undefined.

If the element id resolver object exists above the scope of the global object, the identifier will be shadowed by the global variable object.

Results

Firefox:

Opera 9.5:

Safari 2.0.2, 3.1:

MSIE 7

MSIE 6

Table of Contents