var xx = someNumber.min; |
When I create new web components, I frequently add new attributes not currently associated with any existing html tag.
I originally simply hacked together some code using the Chrome debugger to see what was available. That "solution" worked for every browser I tried .. except for IE 8. The problem there was that (according to the debugger - Tools / Developer Tools) IE 8 does not include the attributes in the DOM structure.
While attempting to debug the problem using other techniques, I discovered that the document object does not support
Browser Differences
<input type="number" id="test_a1" class="mcNumber" min="0" max="100000" value="200" step="1e-9"> |
Screen shots of the debug windows | |
---|---|
Chrome 49 and Windows XP
| IE 8 and Windows XP
|
First try
<input type="number" class="mcnumber" id="mcFFTest_number" size="9" value=100 integer> function mcCheckAttrs(UINumber){ var attr = UINumber.attributes; UINumber.integer = false; if ((attr.integer) && (attr.integer.value != "false")) { UINumber.integer = true; } } |
First IE 8 fix failed
<input type="number" class="mcnumber" id="mcFFTest_number" size="9" value=100 integer> function mcCheckAttrs(UINumber){ UINumber.integer = false; // this causes IE 8 to fail if (UINumber.getAttribute("integer") != null) { UINumber.integer = true; } } |
A better fix
Once I realized that IE 8 would return values not present in the raw html, I tried this - a logical approach.
function mcCheckAttrs(UINumber){ if (UINumber.getAttribute("integer") != null) { UINumber.integer = true; } else { UINumber.integer = false; // placing this here allows all browser to work } } |
Note: If the attribute does not exist, the return value is null or an empty string ("") |
This Works
var mcNum_int = UINumber.getAttribute("integer"); if ((mcNum_int != null) && ((mcNum_int == "") || (mcNum_int == 'true')) ) { UINumber.mcInteger = true; } else { UINumber.mcInteger = false; } |
I am sure that this code will fail in some browser because it assumes that the presence of an empty string indicates that the attribute is there (a clear violation of the w3schools note) - but I have no idea which one. The "logical" way to approach this would be to use the hasAttribute method - except that IE 8 does not support that method.
Write once - debug forever!
Author: Robert Clemenzi