IE6 javascript:void (0);

Source: Internet
Author: User

have encountered this kind of problem several times, summarize now.

Code:

<a onclick= "window.location.href= ' http://www.google.com '" href= "javascript:void (0);" >google</a>
This code in the mainstream browser is not a problem, but under the IE6 will appear can not jump phenomenon.

After a sweep, found that the bound event will be executed, due to the DOM itself bubbling event, it will finally execute the href attribute in the javascript:void (0);

a label

A tag is a label used for page jumps, and its mechanism is to tell the browser URL and jump. Of course, we can execute JavaScript directly in the href genus.

Code:

<a href= "Javascript:alert (' hello! ');" >say hello</a>
Say Hello

This is because JavaScript executes the alert function, but it returns NULL. So a tag does not perform any action.

javascript:void (0);

void (ARG), which can be understood as a function that returns null forever, but whose arguments cannot be null. Its arguments can be arbitrary expressions or even functions.

Code:

<a href= "javascript:void (name = ' would '); alert (' Hello ' + name);" >say hello</a>
Say Hello

conjecture on the operation of IE6

IE6 runs the events that the DOM itself binds to, such as the onclick, and if it does not block bubbling, the href attribute is executed sequentially. and void (0); It is not necessary to perform any events, IE6 tells the browser not to perform any events (overwriting previous actions), and terminating bubbling is equivalent to return false; So the browser does not perform any action.

This will only prevent bubbling events within the onclick event.

The improved code is:

<a onclick= "window.location.href= ' http://www.google.com ' return false;" href= "javascript:void (0);" >google</a>
This will work properly under the IE6.

Other methods

Of course, you can not use Javascript:void (0), and the use of # can also be avoided, the href attribute of the # is meant to be the anchor point #name so when you do not specify any anchor will naturally go to the top of the page and add a hash symbol after the URL.

There's a very special use of anchors here.

When the value of the href attribute is an anchor point, but the anchor point is not found, it is equivalent to returning NULL, so you can use # #来代替javascript: void (0);

Of course, the premise is that your page does not have the anchor name is # yo ~# # # and # # What's the difference? By the # Explanation of the href attribute, # #应该是去寻找 <a name= ' # ' ></a> anchor Point, # # #应该是去寻找 <a name= ' # # ' ></a> anchor, but the Name property should be [ A-za-z] The letter begins and has a unique identity. So this should be just a hack notation, it should be no different. The above is my understanding:)

IE6 javascript:void (0);

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.