SoFunction
Updated on 2025-02-28

Instructions for use of # symbols in URL address

Generally, we want to execute javascript code after clicking on a tag. There are several ways to write it:
Method 1: <a href="#" onclick="alert(1);">Click one</a>
The disadvantage of this method is that after clicking, you will add a # number after the URL in the address bar, and move the page to the top. It is generally not recommended to use it.
Method 2: <a href="javascript:void(0);" onclick="alert(1);">Click one</a>
This method avoids the disadvantages of Method 1 and has no impact on the page after clicking. However, there is a fatal disadvantage, that is, the submit() method of the form object cannot be executed in IE6, nor can the jump statement be executed, for example
<a href="javascript:void(0);" onclick="[0].submit();">Click one</a>
<a href="javascript:void(0);" onclick="='';">Click one</a>
It is invalid under IE6. If you change to href="#" and you can execute normally under IE6.

Method 3: <a href="###" onclick="alert(1);">Click one</a>
Although three # numbers will be added to the URL of the address bar after clicking, 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 JavaScript method you execute needs to submit a form or jump to the page, then use the method three, and use the method two in other cases.

In addition: This also leads to another problem. Generally, reloading the current page with the following javascript code:
= ;
However, when the URL address contains #, the above code is invalid, so the following code is needed:
= ('#')[0];
Split the URL address with # symbol and take the first part.

In many cases /#desc, the subsequent desc may be parameters. The parameters that are not easy to display different contents, not simple anchor points, but ajax reads contents.