我试图让我在这里找到的代码在我的网站上工作。正如预期的那样,该站点是作为一个普通站点构建的,发送页面更改的 HTTP 请求,现在我正在尝试将其升级到完整的 ajax。
下面的代码执行 pushState,后退/前进按钮工作,但我无法更改页面内容。有人知道我要去哪里错了吗?
<script>
$(function(){
$('a').on('click', function(e) {
$("#loading").show();
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) {
$("#loading").show();
console.log("pathname: "+location.pathname);
loadContent(location.pathname);
};
});
function loadContent(url){
// USES JQUERY TO LOAD THE CONTENT
$.getJSON("post.php", {cid: url, format: 'json'}, function(json) {
// THIS LOOP PUTS ALL THE CONTENT INTO THE RIGHT PLACES
$.each(json, function(key, value){
$('#wrapper').load(href);
});
});
}
</script>
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Title</title>
</head>
<body>
<a href="link1.php"></a>
<a href="link2.php"></a>
<div id="wrapper-outer">
<div id="wrapper">
</div>
</div>
</body>
</html>
编辑:
抱歉,链接似乎从未插入。
文章:http ://www.seomoz.org/blog/create-crawlable-link-friendly-ajax-websites-using-pushstate 工作示例:http ://html5.gingerhost.com/