What is javascript: void(0);?


Hey, guys.
There is a website. There is a link with href = "javascript:void(0);". What's it? And how do I find out what the script is referring to and where? There is access to all sources.

Author: Виталина, 2015-01-08

7 answers

void(0) - this is an expression obtained by casting the constant type 0 to void. That is, a simple meaningless expression.

A link in this form simply doesn't do anything. (And this is a popular idiom, by the way.) It is possible that the script on the page modifies href later, so that the link does something more intelligible; but until/if this script does not run, the link will remain as it is.

 34
Author: VladD, 2015-01-09 12:31:00

When the browser clicks on the javascript: link, it replaces the page content with the returned value, except when undefined is returned. The void operator always returns the same value undefined.

Details (in English): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

 14
Author: Sergiks, 2015-01-09 12:43:22

In older browsers, it was possible to create a variable named undefined. Accordingly, to get the undefined value, this construction is used. You can pass any number/type of variable to the void construct-void 123, void [] will also return undefined.

 8
Author: woland, 2015-01-09 08:27:29

A construction of the href = "javascript:void(0);" type is usually used when a js script should work after a normal link click.

Examples of such cases are opening a modal window, sending / receiving data using ajax, or having to check some data using js before clicking on this link.

To find the script that triggers when you click on this link, you should look in the inspector mode. An example of how this can be done, on in the screenshot: Example of how to see a processing js script

 6
Author: vjweb, 2018-01-27 22:15:40

href = "javascript:void(0);" add that when you click on a link that does not perform its direct function, the page does not scroll up, but everything remains in place

 4
Author: Evgeny Palguev, 2019-11-01 15:19:27

In this case, it is the same as return false;

 2
Author: Get, 2015-01-08 20:19:55

Prohibits sending data to the server.

 -2
Author: Kuroch, 2015-01-11 18:30:59