Bending the DOM to your will
Update: I thought I was very clever and posted this before testing fully on IE... Problem is that IE doesn't implement the DOM objects as native JavaScript objects. As a result, you can't simply add methods to their prototypes. I'll keep looking for a solution though.
One of the biggest frustration around JavaScript development is the incompatibilities between different browsers. Internet Explorer is certainly one of the biggest culprit in that area with their not quite complete DOM implementation.
A good example is the Option constructor that is available on Firefox and other well behaved browser. It's an extremely convenient alternative to using the core DOM to build drop down boxes. It just didn't seem to be available on Internet Explorer. I therefore had 2 options:
- Do without. Use the raw DOM API or an innerHTML hack to build the options and add them to the drop down box.
- Implement it in my library so it would be available.
I chose option 2. I now check for the existence of the Option constructor and define it if it's not available.
Another example are areas where the DOM API haven't specified a method to remove all children of a node. It's easy enough to do with some looping, but it does get annoying to have to continually rewrite the code. One of the advantages of JavaScript is that the objects are never closed. This includes their prototype. As a result, I added a removeAllChildren function to the Node's prototype. I can now clean up portions of the DOM tree very effectively now.
JavaScript is a very powerful language, and you do not have to submit yourself to the limitations imposed on you by incomplete API implementations. You can always add what you need and bend the DOM to your will.
Trackbacks
Use the following link to trackback from your own site:
http://blog.fredjean.net/articles/trackback/74
