WebDevelopersNotes logo

home-icon Home / JavaScript / JavaScript Event Handlers – onclick() and ondblclick()

JavaScript Event Handlers – onclick() and ondblclick()

JavaScript Event Handlers - onclick() and ondblclick()

When a link is clicked it takes the user to the page specified in the URL of the HREF attribute. To prevent this from happening, use javascript:void(0) as the value of HREF.

<A HREF="javascript:void(0)"
onmouseover="document.bgColor='#EEEEEE'">
Dead link</A>

Link

You may wonder why we can’t use HREF=”” (a blank or no value) instead of HREF=”javascript:void(0)”. The reason is that a blank or null value for HREF is interpreted as the URL of the directory. Thus, in our case if we use HREF=””, the browser actually sees it as http://www.webdevelopersnotes.com/tutorials/javascript/.
javascript:void(0) returns (not specifies!) a null value. Hence, whenever a link is clicked no page is loaded.

Mouse clicks and event handlers

So far we have only looked at event handlers that capture mouse movements over a link. JavaScript also provides event handlers for links that understand mouse button clicks; onclick and ondblclick. Their usage is similar to other event handlers.

<A HREF="javascript:void(0)" 
onclick="alert('You clicked on the link')">
Dead link</A>

Dead link

<A HREF="javascript:void(0)" 
ondblclick="alert('You double clicked on the link')">
Double click on me</A>

Double click on me

Sponsored Links

Your comments
Star icon IMPORTANT Have a question / problem? Click here to ask an expert.

If it's on the Internet, it must be true.

Sponsored Links

Tips

How do I view a deleted web page?
Is there a way to view a deleted web page - one that is no longer available? Yes there is and the solution is quite simple. [more...]

Fact

Tetris was the very first entertainment software to be imported by the United States from the USSR. In 1987, three years after it's launch in the Soviet Union, the game was released in the United States by Spectrum Holobyte for the IBM PC and Commodore 64, the largest selling computer of all time. Tetris was developed by Alexey Pajitnov, a Russian computer programmer, when he was working for the Dorodnicyn Computing Centre of the Academy of Science. One of his responsibilities was testing the capabilities of new hardware. And for this, Pajitnov came up with the idea of designing a game. Since he was working on the Electronika 60, a text-only terminal, the original Tetris game had letters instead of graphics. The game proved to be a hit with his colleagues and soon became popular in the Soviet Union. Tetris then found its way to Budapest, Hungary, and was picked up by Spectrum Holobyte who developed a version of it and started to market it in the United States. [more...]

We use cookies to give you the best possible website experience. By using WebDevelopersNotes.com, you agree to our Privacy Policy