In general we want to have a tag click to execute JavaScript code, there are the following ways:
mode one: <a href= "#" onclick= "alert (1);" > Click a </a> The disadvantage of this approach is that after clicking the URL in the Address bar, add the # number, while moving the page to the top, generally do not recommend use.
mode two: <a href= "javascript:void (0);" onclick= "alert (1);" > Click a </a> This way avoids the disadvantage of the way one, after clicking on the page without any effect. But there is a fatal drawback is that under the IE6 cannot execute the form object's submit () method, also cannot execute the jump statement, for example
<a href= "javascript:void (0);" onclick= "document.forms[0].submit ();" > Click a </a>
<a href= "javascript:void (0);" onclick= "window.location.href= ' http://www.google.com ';" > Click a </a>
In the IE6 under the invalid, replaced by href= "#" can be in the IE6 under normal execution.
Mode III: <a href= "###" onclick= "alert (1);" > Click a </a> Click on the Address bar after the URL added three #, but will not affect the scroll bar, at the same time under the IE6 can also execute the form object of the Submit () method and jump statement, is a more eclectic approach.
Conclusion: I personally think that if you need to submit a form or jump page in the JavaScript method you execute, use mode three, and all other cases will be two.
Another: This comes with another problem, typically reloading the current page with the following JavaScript code:
Window.location.href = window.location.href;
However, when the URL address contains #, the above code is invalid, so you need to use the following code:
Window.location.href = Window.location.href.split (' # ') [0];
Separate the URL address with the # symbol and take the first part.
In many cases/index.htm#desc, the following desc may be parameters, not easy to display different content, not simple anchor points, but Ajax read content.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.