November 18, 1999 - Cancelling a Link | WebReference

November 18, 1999 - Cancelling a Link

Yehuda Shiran November 18, 1999
Cancelling a Link
Tips: November 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

When the user clicks on a link, the browser normally loads the specified Web page. With JavaScript we can cancel this process:

<A HREF="http://www.docjs.com/" onClick="return false">Doc JavaScript</A>

When the user clicks this link (Doc JavaScript), nothing happens. The onClick event handler returns false, so the specified URL isn't loaded. The following link asks the user to confirm the click:

<SCRIPT LANGUAGE="JavaScript">
<!--
function areSure() {
  return confirm("Are you sure?");
}
// -->
</SCRIPT>
<A HREF="http://www.docjs.com/" onClick="return areSure()">Doc JavaScript</A>

When the user clicks this link (Doc JavaScript) a confirm dialog box pops up. If the user clicks OK, the page http://www.docjs.com/ is loaded. If the Cancel button is pressed, the page isn't loaded.