Href bug in IE

Source: Internet
Author: User

CopyCode The Code is as follows: <Div id = "test">
<A href = "#"> test </a>
</Div>
<Div id = "result"> </div>

<SCRIPT type = "text/JavaScript">
(Function (){
VaR test = Document. getelementbyid ('test ');
Alert (test. innerhtml );

VaR result = Document. getelementbyid ('result ');
Result. innerhtml = test. innerhtml;
Alert (result. innerhtml)
})();
</SCRIPT>

The result shows that the href value of element a in result. innerhtml popped up for the second time in IE6 and IE7 is an absolute path.
In fact, people have encountered these problems early (thanks to the materials provided by yuber ):

  • Getattribute ("href") is always absolute
  • Getattribute href bug
    Above Article The processing scheme has been mentioned, that is, using the getattribute ('href ', 2) method in IE. Microsoft has extended the second parameter for this method, which can be set to 0, 1, and 2. If it is set to 2, the original attribute value is returned.
    Script correction: Copy code The Code is as follows: (function (){
    VaR test = Document. getelementbyid ('test ');
    Alert (test. innerhtml );
    VaR result = Document. getelementbyid ('result ');
    Result. innerhtml = test. innerhtml;
    If (/* @ cc_on! @ */0) {// If IE
    VaR links1 = test. getelementsbytagname ('A ');
    VaR links2 = result. getelementsbytagname ('A ');
    For (VAR I = 0, Len = links1.length; I <Len; ++ I ){
    Links2 [I]. href = links1 [I]. getattribute ('href ', 2 );
    }
    }
    Alert (result. innerhtml );
    })();

    An interesting bug found by hedger Wang was found during the search for this problem: when the new href attribute value is reset in IE, if the link text contains "http: // "or" @ ", the innerhtml is displayed incorrectly and is displayed as the href attribute.
    Solution (shref is the new href value to be set ):Copy codeThe Code is as follows: shref = 'HTTP: // www.hedgerwow.com ';
    VaR ismsie =/* @ cc_on! @ */False;
    If (ismsie ){
    Shref = ''+ shref; // Add extra space before the new href
    };

    Details: Internet Explorer might reset anchor's innerhtml incorrectly when a new "href" is assigned

  • 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.