简体   繁体   中英

Why isn't this JQuery code working on IE8 and IE9?

This function updates my content using the .load method from JQuery. The code works as intended on Google Chrome and Mozilla Firefox, but does not work at all in IE8 nor IE9.

Here is its content:

 $(function(){

   var newHash='';
    $contentwrapper = $("#contentwrapper");

    $("nav").delegate(".menuOptions", "click", function() 
    {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function()
    {
        newHash = window.location.hash.substring(1);  
        $contentwrapper.load(newHash + " #contentcolumn");
        setTimeout(TitleUpdater, 200);
    });
 });

 function TitleUpdater()
 {
     top.document.title=$("#contentcolumn").attr("title");
 }

I would like to know how to make this code compatible with Internet Explorer! Thank you.

Edit: On Firefox and Chrome, this code only replaces one big <div id="contentcolumn">...</div> and all the style around stays the same as it was before the script execution. While IE8 and IE9 just remove ALL my html code by <div id="contentcolumn">...</div> (it even writes over the head and body tags). This is really weird!

Here is index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Vladi Manaev</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/bagums.css" media="screen" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>

    <body>
        <div id="maincontainer">

            <div id="topsection">
                <div id="banner"></div>
                <div class="navbar">
                    <div class="navbar-inner">
                        <div class="container">
                            <nav> 
                                <a class="brand" href="">Bagums.com</a>
                                <ul class="nav">
                                    <li class="dropdown">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Basic Tutorials<b class="caret"></b></a>
                                        <ul class="dropdown-menu">
                                            <li><a class="menuOptions" href="BasicTutorials/CTutorial.html" name="CTutorial">C</a></li>
                                            <li><a class="menuOptions" href="BasicTutorials/CppTutorial.html" name="CppTutorial">C++</a></li>
                                            <li><a class="menuOptions" href="BasicTutorials/CsharpTutorial.html" name="CsharpTutorial">C#</a></li>
                                            <li><a class="menuOptions" href="BasicTutorials/JavaTutorial.html" name="JavaTutorial">Java</a></li>
                                            <li><a class="menuOptions" href="BasicTutorials/OtherTutorials.html" name="OtherTutorials">Others</a></li>
                                        </ul>
                                    </li>
                                    <li class="dropdown">
                                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">My Projects<b class="caret"></b></a>
                                        <ul class="dropdown-menu">
                                            <li><a class="menuOptions" href="MyProjects/CProject.html">C</a></li>
                                            <li><a class="menuOptions" href="MyProjects/CppProject.html">C++</a></li>
                                            <li><a class="menuOptions" href="MyProjects/CsharpProject.html">C#</a></li>
                                            <li><a class="menuOptions" href="MyProjects/JavaProject.html">Java</a></li>
                                            <li><a class="menuOptions" href="MyProjects/Others.html">Others</a></li>
                                        </ul>
                                    </li>

                                </ul>

                                <ul class="nav pull-right">
                                    <li class="dropdown-toggle"><a class="menuOptions" href="OtherPages/Links.html">Links</a></li>   
                                    <li class="dropdown-toggle"><a class="menuOptions" href="OtherPages/Contact.html">Contact</a></li>
                                    <li class="dropdown-toggle"><a class="menuOptions" href="OtherPages/About.html">About</a></li>
                                </ul>
                            </nav>
                        </div> <!-- container -->
                    </div> <!-- navbar-inner -->
                </div><!-- navbar -->
            </div><!-- topsection -->

            <div id="contentwrapper">
                <div id="contentcolumn" title="Vladi Manaev">
                    <div id="mainPostsTitle">Latest News</div>

                    <div class="postWrapper">
                        <div class="post">
                            <div class="postTitle"><div class="postTitleTxt">Third post on website</div></div>
                            <div class="postInfo"> Aug 4, 2012 @ 7:23 pm</div>
                            <div class="postContent">
                                <p>
                                    CONTENT
                                </p>
                            </div>
                        </div>
                    </div>

                    <div class="postWrapper">
                        <div class="post">
                            <div class="postTitle"><div class="postTitleTxt">Second post on website</div></div>
                            <div class="postInfo"> Aug 4, 2012 @ 7:22 pm</div>
                            <div class="postContent">
                                <p>
                                     CONTENT
                                </p>
                            </div>
                        </div>
                    </div>

                    <div class="postWrapper">
                        <div class="post">
                            <div class="postTitle"><div class="postTitleTxt">First post on website</div></div>
                            <div class="postInfo"> Aug 4, 2012 @ 6:48 pm</div>
                            <div class="postContent">
                                <p>
                                       CONTENT
                                </p>
                            </div>
                        </div>
                    </div>

                </div><!-- contentcolumn -->
            </div><!-- contentwrapper -->

            <div id="footer"> 
                <nav>
                    <div> Copyright &copy; 2012 created by <a class="menuOptions" href="OtherPages/Contact.html"> Vladi Manaev</a></div>
                </nav>
            </div><!-- footer -->

        </div><!-- maincontainer -->

        <!-- Placed at the end of the document so the page load faster -->
        <script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
        <script type="text/javascript" src="scripts/bootstrap-dropdown.js"></script>


    <script>
     $(function(){

       var newHash='';
        $contentwrapper = $("#contentwrapper");

        $("nav").delegate(".menuOptions", "click", function() 
        {
            window.location.hash = $(this).attr("href");
            return false;
        });

        $(window).bind('hashchange', function()
        {
            newHash = window.location.hash.substring(1);  
            $contentwrapper.load(newHash + " #contentcolumn");
            setTimeout(TitleUpdater, 200);
        });
     });

     function TitleUpdater()
     {
         top.document.title=$("#contentcolumn").attr("title");
     }

</script>

    </body>
</html>

When i changed my href to start with # like this:

<a class="menuOptions" href="#BasicTutorials/CTutorial.html" name="CTutorial">C</a>

...

It start working on IE as well! My guess is that window.location.hash does not work on IE... and when i added "#" by hand it solved the problem.

Thanks everyone for trying to help !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM