开发者

Help me understand javascript:void(null)

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)" >A开发者_如何学Pythonccept Data</a>


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.


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.


javascript:void(null) = do nothing.

Notice there is a javascript call in the onclick event handler - that does something (I'm guessing it accepts data by processing the response ;).


Appending void(0) to javascript instructions is a common trick when you use a javascript: pseudo-URL to run code. If you omit doing this, and the script returns something other than undefined, it will be treated as if it was passed to document.write - that is, the browser will navigate away to an empty page.

There are valid applications for this trick (namely, bookmarklets should always end like this), but in the example you gave it is just wrong, for reasons already explained by others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜