不久前有一个类似的问题,我想出了以下解决方案。
您的 url 应该指向真实页面,以使其适用于 js 禁用用户。点击处理程序应该处理 ajax 请求。 Hash 应该包含 url,以及像&ajax 这样的部分来指示请求的类型。
如果请求来自 ajax,只需发送内容。如果不是,则将内容包装到页眉和页脚中以响应整个站点。
Urls 应该与链接到 ajax 生成的哈希并将它们用作链接一起使用。整个想法基本上是模仿你在 facebook 上看到的那种行为。
Javascript
// click handler for ajax links
function goToWithAjax(hash) {
hash = hash.href ? hash.getAttribute("href", 2) : hash;
ajax( hash, function( response ) {
document.getElementById("content").innerHTML = response;
});
hash = ("#!/" + hash).replace("//","/");
window.location.hash = hash;
return false;
}
.htaccess
auto_prepend_file = "prepend.php"
auto_append_file = "append.php"
前置
$url = $_SERVER['REQUEST_URI'];
$parts = explode('#!', $url);
$hash = isset($parts[1]) ? $parts[1] : false;
// redirect if there is a hash part
if ($hash) {
header("Location: $hash");
}
// find out if it's an ajax request
$ajax = strstr($url, "&ajax");
// we need header if it's not ajax
if (!$ajax) {
get_header();
}
追加
// we need footer if it's not ajax
if (!$ajax) {
get_footer();
}
get_header()
function get_header() {
echo <<< END
<html>
<head></head>
<body>
<div id="page">
<div id="header">
<div id="logo"></div>
<ul id="nav">menu...</ul>
</div>
<div id="content">
END;
}
get_footer()
function get_footer() {
echo <<< END
</div> <!-- end of #content --->
<div id="footer">(c) me</footer>
</div> <!-- end of #page --->
</body>
</html>
END;
}