pushState重新加载页面

时间:2013-06-29 00:08:36

标签: javascript jquery html html5

我试图效仿此处使用的示例:http://html5.gingerhost.com/ 我没有使用JSON,而是决定使用jQuery的.load()代替

这是我的代码:

    $(function() {
        $('a').click(function(e) {
            $('img#logo').stop().hide().fadeIn( "slow" );

            href = $(this).attr("href");
            loadContent(href);
            // HISTORY.PUSHSTATE
            history.pushState('', 'New URL: '+href, href);
            e.preventDefault();         
        });

        // THIS EVENT MAKES SURE THAT THE BACK/FORWARD BUTTONS WORK AS WELL
        window.onpopstate = function(event) {
            console.log("pathname: "+location.pathname);
            loadContent(location.pathname);
        };
    });

    function loadContent(url){
        // USES JQUERY TO LOAD THE CONTENT
        $('#load').load( url + " #load2").stop().hide().fadeIn( 'slow' );   
    }

示例的loadContent函数:

        function loadContent(url){
        // USES JQUERY TO LOAD THE CONTENT
        $.getJSON("content.php", {cid: url, format: 'json'}, function(json) {
                // THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES
                $.each(json, function(key, value){
                    $(key).html(value);
                });
                $("#loading").hide();
            });

        // THESE TWO LINES JUST MAKE SURE THAT THE NAV BAR REFLECTS THE CURRENT URL
        $('li').removeClass('current');
        $('a[href="'+url+'"]').parent().addClass('current');

    }

HTML:

<head> 
    <meta charset="utf-8"> 
    <title>Andy Simon</title>

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>                   
    <script type="text/javascript" src="javascript.js"></script>

    <link rel="stylesheet" type="text/css" href="style.css">
    <link href='http://fonts.googleapis.com/css?family=Raleway:100' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="main">
    <div id="top">  
        <div id="logo">
            <div>
            <a id="logo" href="index.html"><img id="logo" src="logo1.png" /></a><span id="logo_text">simon</span>
                </div>
        </div>
        <div id="nav">  
            <a class="nav_link" id="work" href="work.html"> work . </a> 
            <a class="nav_link" id="work" href="work.html"> about . </a>
            <a class="nav_link" id="work" href="contact.html"> contact </a>                 
        </div>  
    </div>

    <div id ="middle">

        <div id="load">
            <div id="load2">
                home home andy pandy home home home
            </div>                      
        </div>  

    </div>  

    <div id="foot">     
    </div>

</div>
</body>

当我点击链接时,页面会重新加载,这正是我想要阻止的。我以为我的e.preventDefault()会阻止它。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

你可以把'返回假;'在您的js代码中阻止它上传:

    $('a').click(function(e) {
        e.preventDefault();
        $('img#logo').stop().hide().fadeIn( "slow" );

        href = $(this).attr("href");
        loadContent(href);
        // HISTORY.PUSHSTATE
        history.pushState('', 'New URL: '+href, href);
        return false;
    });