JS Void


Javascript void is an operator for return a null value, especially useful in the hyperlinks to avoid loading new pages.

Here is a typical hyperlink:

<a href="http://www.endmemo.com">Home</a>

Home, If you click the link, the home page will be loaded.

We can use a hyperlink execute javascript, rather than loading a new web page:

<a href="javascript: alert('JS');">JS Link</a>

JS Link

Use void to assign value:

<a href="javascript:void(x=5);alert(x);">JS Link</a>

Link

void(0) to avoid page loading:

<a href="javascript:void(0)"
ondblclick="alert('dblclick')">Link</a>

Link

Without void(0), page reloaded by a click:

<a href="" ondblclick="alert('dblclick')">Link</a>

Link

void(0) provides a null value for href, thus avoids the unnecessary page loading.

endmemo.com © 2024  | Terms of Use | Privacy | Home