[thelist] Brain Addled Javascript....

.jeff jeff at members.evolt.org
Fri Jun 22 18:40:20 CDT 2001


: From: Anthony Baratta
: I should have tried the onClick solution, but
: was too frustrated.

that's ok.  that happens.

: I have a question about your answer in the
: archives:
: [a href="JavaScript://"
:   onClick="alert('Hi'); return false"
: ]Click me![/a]
: I'm confused by the part:
: ...href="JavaScript://"...
: What is this??

let's break it down piece by piece.  you'll kick yourself afterwards, it's
so simple.  *grin*

the full href attribute value:


JavaScript: = the js protocol

// = js comment

so basically i'm telling the browser to execute a javascript comment which
does absolutely nothing.  in my experience, this is the preferred method of
making a link that does nothing when clicked (with the exception of any
script in the onclick event handler).  non-js browsers don't do anything.
js-enabled browsers don't throw an error.  it's preferred over using the
void(0) method because that was introduced in js1.1 and therefore not
supported by js1.0 only browsers.  this is the same sort of thing, but
supported by all js-enabled browsers, produces the same effect in all
browser/os combinations, doesn't jump the page back to the top (like using
#), and doesn't require you to initialize any special dummy functions.

fwiw, i've also seen people use a semi-colon instead of the javascript
comment.  that also works, but is much harder to spot in your code.  see for
yourself, which one stands out more that it's not just the javascript
protocol in the href?

<a href="JavaScript://">

<a href="JavaScript:;">

good luck,


jeff at members.evolt.org

More information about the thelist mailing list