Home  >  Article  >  Web Front-end  >  Instructions for using # symbol in URL address_javascript skills

Instructions for using # symbol in URL address_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:10:461329browse

Generally, we want an a tag to execute javascript code after being clicked. There are several ways to write it:
Method 1: Click one
The disadvantage of this method is that after clicking It will add a # sign after the URL in the address bar and move the page to the top. This is generally not recommended.
Method two: Click one
This method avoids the shortcomings of method one and has no impact on the page after clicking. But there is a fatal shortcoming, that is, the submit() method of the form object cannot be executed under IE6, nor can it execute jump statements, such as
Click one
Click one
It will not work under IE6. If you replace it with href="#", it will work normally under IE6.

Method 3: Click
Although three # signs will be added after the URL in the address bar, it will not affect the scroll bar. At the same time, The submit() method and jump statement of the form object can also be executed under IE6, which is a relatively compromise method.

Conclusion: I personally think that if the executed javascript method needs to submit a form or jump to a page, use method three. In other cases, use method two.

Another: This leads to another problem. Generally, the following javascript code is used to reload the current page:
window.location.href = window.location.href;
But when the URL address is When containing #, 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 , just take the first part.

In many cases /index.htm#desc, the following desc may be the parameter. The parameter that is not easy displays different content, not a simple anchor point, but an ajax read content.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn