AJAX を使用してページの一部を更新している間に、Lifehacker が URL を変更できることがわかりました。HTML5 または history.js プラグインを使用して実装できると思いますが、ライフハッカーはどちらも使用していないと思います。
彼らがどのようにそれを行うかについての手がかりを持っている人はいますか? 私は AJAX を初めて使用し、Ajax を使用してページの一部を更新することができました。
詳細なステップバイステップのアルゴリズムについて、@Robin Andersonに感謝します。私はそれを試してみましたが、うまく機能しています。ただし、本番環境でテストする前に、私が持っているコードを実行したいと思います。私はすべて正しく行いましたか?
<script type="text/javascript">
var httpRequest;
var globalurl;
function makeRequest(url) {
globalurl = url;
/* my custom script that retrieves original page without formatting (just data, no templates) */
finalurl = '/content.php?fname=' + url ;
if(window.XMLHttpRequest){httpRequest=new XMLHttpRequest}else if(window.ActiveXObject){try{httpRequest=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{httpRequest=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}}}
/* if no html5 support, just load the page without ajax*/
if (!(httpRequest && window.history && window.history.pushState)) {
document.href = url;
return false;
}
httpRequest.onreadystatechange = alertContents;
alert(finalurl); /* to make sure, content is being retrieved from ajax */
httpRequest.open('GET', finalurl);
httpRequest.send();
}
/* for support to back button and forward button in browser */
window.onpopstate = function(event) {
if (event.state !== null) {
document.getElementById("ajright").innerHTML = event.state.data;
} else {
document.location.href = globalurl;
return false;
};
};
/* display content in div */
function alertContents() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var stateObj = { data: httpRequest.responseText};
history.pushState(stateObj, "", globalurl);
document.getElementById("ajright").innerHTML = httpRequest.responseText;
} else {
alert('There was a problem with the request.');
}
}
}
</script>
PS: コメントにコードを貼り付ける方法がわからないので、ここに追加しました。