# Symbol in the URL

Source: Internet
Author: User

Generally, we want a tag to be clicked and then execute JavaScript.Code, There are several ways to write:
Method 1: <A href = "#" onclick = "alert (1);"> click 1 </a>
The disadvantage of this method is that after clicking it, # is added to the URL of the address bar, and the page is moved to the top, as shown in:

Method 2: <A href = "javascript: void (0);" onclick = "alert (1);"> click 1 </a>
This method avoids the disadvantage of method 1. Clicking this method does not affect the page. However, there is a fatal drawback: in IE6, the submit () method of the form object cannot be executed, nor jump statements can be executed, such
<A href = "javascript: void (0);" onclick = "document. Forms [0]. Submit ();"> click one. </a>
<A href = "javascript: void (0);" onclick = "window. Location. href = 'HTTP: // www.google.com ';"> click one. </a>
In IE6, it will be invalid. If it is changed to href = "#", it can be executed normally in IE6.

Method 3: <A href = "###" onclick = "alert (1);"> click 1 </a>
After clicking the button, three # numbers are added to the URL in the address bar, but the scroll bar is not affected. In IE6, the submit () method and jump statement of the form object can also be executed, is a relatively discounted method.

Method 4: <A href = "javascript: Alert (1);"> click one. </a>
Clicking the button will not affect the scroll bar, nor add three # numbers after the URL in the address bar. At the same time, the form object's submit () method and jump statement can also be executed under IE6; however, the biggest drawback of writing this code is that the JS methods are

In the event of Mouse clicking, the syntax is not flexible, and other events cannot be used. This restricts the scope of use and does not allow you to dynamically bind tags to events.

Conclusion: I personally think that if the JavaScript method to be executed needs to submit a form, jump to the page, use method 3, and use method 2 in other cases.

Another: This introduces 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 contains #, the above Code is invalid, so you need to use the following code:
Window. Location. href = Window. Location. href. Split ('#') [0];
Split the URL address with the # symbol and take the first part.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.