JavaScript setAttribute vs IE

Published 19 August 04 by Justin French

I had a neat little DOM script which basically looped through all <a> tags in the document looking for those which had a href attribute and a rel attribute set to "help". Example:

<a href='help/something/' rel='help'>help</a>

DOM scripting would then add an onclick event handler to it which popped up the help URL in a new (smaller) window.

In every browser I bothered to test (except IE/Win) the following bit of code (not the complete function) worked perfectly:

anchor.setAttribute("onclick",window.open(...))

After a some Googling, it would appear that IE doesn’t play that way. It has it’s own proprietary method attachEvent, but we’re aiming for standards here, so I had to find another way.

Instead of anchor.setAttribute(...), we use anchor.onclick = function() { ... }. It’s not as clean, but it works.

So far, everything works great. Users with JavaScript get a nice little pop-up window, and users without it get the link in the current window (or in a blank window if we were to include target='_blank').

Hopefully this will save others some time in the future. You can view a complete working example of the function in action here.

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search