Pajax-1

ajax無刷新頁面同時改變url

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>The History API</title>
</head>
<body>
    <h1 id="number">1</h1>
    <a id="forward" href="?num=2">Go forward!</a>
</body>
<script type="text/javascript" charset="utf-8">
    var useHash = false;
    var hashExp = /#([0-9]+)/;
    
    if(!history.pushState) {
        useHash = true;
    }
    
    var link = document.getElementById('forward');
    var num = document.getElementById('number');
    
    //consolidate the update into one place
    function handleStateChange(count) {
        num.innerHTML = count;
        document.title = 'Number ' + count;
        link.href = '?num=' + (parseInt(count,10) + 1);
    }
    function setNumFromUrl() {
        if(location.search) {
            
            var match = location.search.match(/num=([0-9]+)/);
            if(match) {
                
                //if pushState doesn't work, we need to 
                //scrub the query string and redirect to the hash version
                if(useHash) {
                    location = 'history.html#' + match[1];
                    
                } else {
                    handleStateChange(match[1]);
                }
            }
            
        }else if (location.hash) {
            
            var match = location.hash.match(hashExp);
            
            
            handleStateChange(match[1]);
            
            //if the user can use push state, but came with a hash url, 
            //we can upgrade the url with replaceState.
            if(!useHash) {
                history.replaceState({count:match[1]}, null, 
                            'history.html?num=' + match[1]);
            }
            
        } else {
            handleStateChange(1);
        }
    }
    
    link.addEventListener('click', function(e) {
        var myNum;
        
        e.preventDefault();
        myNum = parseInt(num.innerHTML, 10);
        myNum++;
        
        if(useHash) {
            
            location.hash = myNum;
            
        } else {
            
            history.pushState({count:myNum}, null, '?num=' + myNum);
            
        }
        
        handleStateChange(myNum);
        
    });
    
    //because the first popstate isn't called,
    //we need to call this manually
    setNumFromUrl();    
      //綁定監(jiān)聽事件
    if(!useHash) {
        //this is the lightweight bversion
        addEventListener('popstate', function(e) {
            if( e.state && e.state.count ) {
                handleStateChange(e.state.count);
            } else {            
                setNumFromUrl();            
            }       
        });
        
    } else {
        //we need to know the old value
        //to be able to see if ti changed
        var oldHash = location.hash;
        //不支持事件用setInterval監(jiān)控location.hash的變化
        //poll every 100ms
        window.setInterval(function(){
            var match;
            
            if( window.hash !== oldHash ){
                match = location.hash.match(hashExp);
                oldHash = location.hash;
                if(match) {
                    handleStateChange(match[1]);
                }
                
            }
            
        }, 100);
    }
</script>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容