Damon Cortesi's blog

Musings of an entrepreneur.

Javascript Compliance and Oddity

| Comments

Microsoft did a horrible thing for online standards when it allowed Internet Explorer (IE) to accept javascript code that wasn’t fully compliant. Sure, things are easier for developers…at first, until somebody wants to use a different browser.

For example, I ran into some code today that set the value of a text input in a form:

date_input.value=’10/07/2004’

That’s pretty easy and intuitive, but it only works in IE. Want to get it to work elsewhere? Here’s the updated code:

document.getElementsByName(‘date_input’)[0].value=’10/07/2004’

whoa Notice two things:

  1. getElementsByName is plural, getElementByName does not work.
  2. Because you must use the plural, an array is returned and you have to reference the first value in the array by using [0]
Other than the plurality confusion, I would much rather sacrifice easy-to-write code for cross-browser compatibility!

Comments