Remember when we explored window.status
in part 3 of our tutorial? We said the
following:
<A HREF="err.htm" onClick="status='You clicked me';return false">
meticulously returning a false value. Such obsession
with falsehood is justified for all events. If you don't return true, the
action associated with the click event is cancelled. You can see that you
can prevent a click on a link from ever succeeding by simply
inserting the following code into an anchor:
onClick="return false"
(You can then use the anchor for other purposes!)
An even more devious amendment is the following:
<A HREF="javascript:void(0)" onMouseOver="status='Go to Hell';">
What does this do? It tells the browser that it must invoke
javascript (one can do this sort of thing in an HREF), and the
void(0) then says 'do nothing'! This sort of trickery prevents
the link from ever being followed. aVoid it! The void() function
is not guaranteed to exist in all JavaScript-enabled browsers. If
you must use this sort of trickery then:
- First define a function called, say, aVoid() that does nothing!
- Don't put the javascript: instruction inside the href.
Rather say something like:
<A HREF="err.htm" onClick="this.href='aVoid()';status='Go to Hell';">
- You can even have an error page (err.htm) that says "Woops. JavaScript error"
as a backup!
Another option to prevent the HREF from going anywhere is to point
the HREF to the current position (make an anchor) on the current page!