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>
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()
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.
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).
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 void
ed, 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With