gpt4 book ai didi

php - 实现 Web 2.0 导航系统的最佳方式

转载 作者:行者123 更新时间:2023-11-30 23:45:13 25 4
gpt4 key购买 nike

现在这是我的情况:我正在制作一个 CMS。单击链接时,我希望使用 Ajax 动态加载页面。链接中的问题!

实时更改地址栏中地址的唯一方法是使用 anchor 标记。但 PHP 没有获取 anchor 标记,因此我无法使用 PHP 在网站加载时加载页面内容。如果我使用查询字符串加载页面,则单击链接时无法在地址栏中更新查询字符串,因为这会重新加载页面。

我想 Javascript 可以检查地址,将 anchor 标记保存在 cookie 中并重新加载页面,但我宁愿不必这样做。

有谁知道这个问题的解决办法吗?

最佳答案

不久前有一个类似的问题,我想出了以下解决方案。

您的网址应指向真实页面,以便禁用 js 的用户可以使用它。单击处理程序应该处理 ajax 请求。 Hash 应该包含 url,以及像 &ajax 这样的部分来指示请求的类型。

如果请求来自 ajax,只需发送内容即可。如果不是,请将内容包装到页眉和页脚中,以完整的站点进行响应。

Url 应该能够链接到 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;

}

关于php - 实现 Web 2.0 导航系统的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3251263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com