Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help me understand javascript:void(null)

Tags:

javascript

can someone please help me with this javascript:void(null) I found it used in link buttons as follows

<a onclick="ProcessResponse()" href="javascript:void(null)" >Accept Data</a>
like image 888
Anil Namde Avatar asked May 19 '10 08:05

Anil Namde


People also ask

What does JavaScript void () mean?

What is the void keyword? When a function is void, it means that the function returns nothing. This is similar to functions in JavaScript which return undefined explicitly, like so: function und() { return undefined } und()

How do you use JavaScript void?

If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “javaScript:void(0)”, returns the undefined primitive value. The void operator is used to evaluate the given expression. After that, it returns undefined.

Is JavaScript void 0 Safe?

Generally, you want to avoid href="javascript:void(0)" , as it will cause the browser to parse the value of the link URL, which is both costly and unnecessary. It also introduces a potential XSS security vulnerability, as javascript: URLs violate Content Security Policy (CSP).


2 Answers

void is a JavaScript operator but is sometimes mistaken for a function because of the common use of brackets that follow it. The intended purpose of void is to evaluate an expression without returning a value. Therefore, any expression at all can be voided, it doesn't have to be null and quite often you see void(0) or less frequently, void 0.

When you use javascript: in a href attribute, the expression following will be evaluated and its result will be returned. This can be seen by entering the following into your browser address box:

javascript:prompt("test");

Type anything into the box that appears and press enter/click ok. You will notice that the page will disappear and whatever you typed will appear. Now watch what happens if we add void 0;:

javascript:prompt("test"); void 0;

After clicking OK in the prompt, nothing happens. That's void 0's handywork, it returns undefined and so the browser does nothing about it. This applies to href in links too (feel free to try it). The whole thing could even just be written as javascript:void prompt("test");.

As others have mentioned, it's best to use return false; from an event handler rather than using void in the href. In fact, it's recommended not to use javascript: in the href attribute at all.

like image 142
Andy E Avatar answered Sep 19 '22 16:09

Andy E


Basically what happens is the onclick the function ProcessResponse() is called and the href is set to javascript:void(null) to disable the default behaviour of the link.

Most developers are simply used to writing this too:

<a onclick="ProcessResponse(); return false;" href="#" >Accept Data</a>

Example:

Suppose we have this link in place:

<a onclick="ProcessResponse(); return false;" href="http://www.google.com" >Accept Data</a>

Note that href is set to www.google.com but when you actually click on that link, it would simply call the ProcessResponse() function and won't go to www.google.com because the return false put after ProcessResponse() disables the default behavior of the link that is going to www.google.com. Same is the case for the link you have posted.

like image 39
Sarfraz Avatar answered Sep 19 '22 16:09

Sarfraz