Ornamental href question

Comments

I use href="javascript:void 0;" as it was written ever thus in the O'Reilly JS book many moons ago. Also, I like the js 'void' statement.
Thanks for another idea Chris! I read that the href="javascript:void(0);" has problems with IE. I am also a bit confused about what the void does. Ideally I'd like to put something in the href that does nothing at all, but I thought putting void might actually void the javascript if it was trying to open a window.

I have not tried the way you suggest without the parenthesis but I will ad that to my test page! Thanks for your help!
I'm impressed with your curiousity about 'void'. The problem with the solution at the page you linked is that the click will be "killed" and thus will not be heard by any interested js code listening for clicks.

I've never had any issue with the 'void' approach. For design and usability reasons, I definitely would never want to do what the author of the solution you liked to does: Replace a standard UI element such as a checkbox with a toggled pair of images. Jakob Nielsen has some writings on that type of thing that explain better.

And finally, when IE is set to "check every time" for Web content (as you know, a non-standard setting that developers often use to avoid having to explicitly force-refresh), it has problems with showing images. This is most notorious with background images, but I have experienced the same thing with foreground images. In other words, I don't think the 'void' necessarily will have the effect the author describes for the likely 99.9% of users who do not alter that cache default.

About 'void': After a little googling I turned up an entry quoting the creator of JavaScript that seems to verify definitively that 'void' is meant to be used in 'javascript:' pseudo-urls. On the finer points: The parentheses are not needed, as 'void' is a bona fide js operator, and not a method call. Since it's not a method call, I don't agree with the stylistic preference expressed in the Mozilla Developer Center docs on 'void'.

Could this be much ado about void? ;)

[this is good]
Over the last 9 years I have tried several things, but, even though I don't like it, I always came out at href="javascript:" as only option that never caused any drawback or problem.
A more extensive solution that I sometimes use is that I made a custom function that attaches all desired behaviour (colour of the link, action, hover behaviour) as event-handlers to a stripped anchor:
The comment above apparently broke on the lesser than sign... here a retry with the 2nd sentence completely and no lesser than signs:

"A more extensive solution that I sometimes use is that I made a custom function that attaches all desired behaviour (colour of the link, action, hover behaviour) as event-handlers to a stripped anchor: (a id='someId'). This overcomes IE from not recognizing it as an anchor and consequently hover not working. Advantage is that all event-handlers are separated from the html, and there is no bogus href with javascript in the a anymore. But maybe there are other cons, outside the fact it requires more programming."
Thanks Paul,

It's interesting that we both ended up with almost the same solution (href="javascript:" plus or minus a semicolon).

The real-life instance I am using this for has pages with many links with different appearances and behaviors. I am keeping the display of the links (and their hover) in CSS. The markup is generated in an off-line process which determines where the links go and their behavior. I prefer to have the links created with a class (for for appearance) and the actions already in them rather then trying to attach the behavior at runtime. Partly that's just my preference because I like CSS and would rather use it then JavaScript given a choice.

Post a comment

Already a Vox member? Sign in

Miriam

About Me

Miriam
United States
Saving Lives With CSS

Archives