The javascript: void (0) label is displayed in many website A labels. We know that clicking is not going to jump to the link, and there is no response, which is usually followed by javascript: void (0) is followed by a js event. Let's take a look.
First look at the example
The Code is as follows: |
Copy code |
<A href = "Javascript: changeNum (); void (0);"> here </a> <Script> Function changeNum () { Alert ('www. bKjia. c0m '); } </Script> |
When we click it, www. bKjia. c0m will pop up. The rational connection is to the specified link address, so where is javascript: void (0) Holy?
In JavaScript, void is an operator that specifies to calculate an expression but does not return a value.
The format of the void operator is as follows:
1. javascript: void (e-xpression)
2. javascript: void e-xpression
E-xpression is a standard JavaScript expression to be calculated. Parentheses outside the expression are optional, but it is a good habit to write. You can use the void operator to specify hyperlinks. The expression is calculated but not loaded with any capacity in the current document.
The following code creates a hyperlink. Nothing happens after you click it. When a user clicks a link, void (0) is calculated as 0, but it has no effect on JavaScript.
The Code is as follows: |
Copy code |
<A href = "javascript: void (0)"> click here. </a> |
The following code creates a hyperlink. When you click it, the form is submitted.
The Code is as follows: |
Copy code |
1 <a href = "javascript: void (document. form. submit ()"> click here to submit a form </a> |
When using javascript, we usually call the javascript method through a pseudo link in a similar way.
The Code is as follows: |
Copy code |
<A href = http://www.bKjia. c0m/"#" onclick = "javascript: method; return false;"> submit </a> <A href = http://www.bKjia. c0m/"javascript: void (0)" onclick = "javascript: method; return false;"> submit </a> <A href = http://www.bKjia. c0m/"# this" onclick = "javascript: Method"> submit </a> |
# The default anchor that contains a location information is # top, that is, the top of the Web page. javascript: void (0) only indicates a dead link. This is why sometimes a long page browsing link is clearly # But the page jumps to the top, while javascript: void (0) is not so, so it is best to use void (0) when calling the script ). Therefore, we use void (0) When refreshing pages, which is usually Ajax. If you look at the Ajax web page, you will usually see many void (0 ), so before using void (0), You 'd better first think about whether the page needs to be refreshed as a whole.
Summary
1. javascript: void (0) is written in the href of html; return false is written in js (including the onclick element in html). Similar to this, there is event. preventDefault (), preventDefault is a standard event, and return false is mainly used to solve ie;
2. To be honest, at least I have not found any drawbacks in actual operation. If it is hard to say, it should be href = "javascript: void (0)" which is ugly in the page; return false is that if the page js fails, click the link to jump directly to the top (because href = "#");
3. Of course, I think return false (preventDefault) is more reasonable, because javascript: void (0) is really dazzling in the page. Of course, this execution may also affect the js performance of the page, but I have not found any relevant information.