Normally, when you click a link, the browser loads the page specified in the URL of the HREF attribute.Sometimes, you may need to call some JavaSript from within a link. You can accomplish this by using javascript:void(0). For example you want to open a new window when you click on the following link.

Javascript void 0 example

Javascript Code:

<a href=”javascript: void(0)” onClick=”NewWin=window.open(‘http://www.itechies.net/tutorial_images/jscript/menu.html’,’NewWin’, ‘toolbar=yes,status=yes,width=500,height=300,scrollbars=yes’); “>Javascript void 0 example</a>

You may be thinking you can get the same result by using HREF=”” (a blank or no value) instead of HREF=”javascript:void(0)”. When you use a blank or null value for HREF , it is interpreted as the URL of the directory. To explain more , in our case if we use HREF=””, the browser actually sees it as http://www.itechies.net/tutorials/jscript/index-pid-void.htm.

See the 2 links below.

Reload the page and open new window

Don’t reload the page and open window

In the first link, we didn’t specify a URL, but the browser refreshes the current page. To prevent this unwanted action you need to use the void function on such statement, with the second link, we specified “void(0)”, which prevents the browser from reloading. javascript:void(0) returns a null value. This means that when the browser attempts to load a new page it sees null and has nothing to load.

Leave a Comment