JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
 
 

Link.onclick() Handler

Name

Link.onclick() Handler---invoked when a Link is clicked

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Synopsis

<A onClick="handler-statements">    a definition of the handler
<AREA onClick="handler-statements"> another definition; Navigator 3.0 only
link.onclick  a reference to the handler
link.onclick();   an explicit invocation of the handler

Description

onclick() is an event-handler function invoked when the user clicks on a hypertext link. This event handler is defined by the onClick attribute of the HTML <A> or <AREA> tag that defines the hypertext link. The value of this attribute may be any number of JavaScript statements, separated by semicolons. These statements will be executed when the user clicks on the link.

The onclick() event handler is invoked before the browser follows the clicked hypertext link. This allows you to set the href, target, and other properties of the link dynamically (using the this keyword to refer to the clicked link). You may also use Window.alert(), Window.confirm(), and Window.prompt() from this event handler.

In Navigator 3.0, you may prevent the browser from following the link by returning false. If you return true, or any other value, or nothing, then the browser will follow the link as soon as onclick() returns. You might stop the browser from following a link if you used the Window.confirm() method to ask the user if they really want to follow the link and they choose the Cancel button, for example. In general, if you want a link that performs some action but does not cause a new URL to be displayed, it is better to use a the onclick() event handler of a Button object instead of the onclick() handler of a Link object.

Note that while this event handler returns false to tell the browser not to perform its default action (following a link), the onmouseover() event handler must return true to tell the browser not to take its default action (displaying the URL of the link). This incompatibility exists for historical reasons. The standard for Form and form element event handlers is to return false to prevent the browser from performing a default action.

Bugs

In Navigator 3.0, the onclick() event handler of the <AREA> does not work on Windows platforms. Because this is a very common platform, you should avoid using this event handler in client-side image maps. Instead, you can often specify a javascript: URL as the value of the HREF attribute of the <AREA> tag.

See Also

"Link"


Previous Home  
Link.href Book Index  

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell