防止刷新SSI

时间:2012-10-08 22:43:29

标签: server-side-includes

我有一些SSI在我的网站中的许多页面上重复 - 特别是我的导航树允许用户展开或折叠树中的节点。

我想阻止加载相同的.shtml文件,如果它已经在上一页中加载了 - 这样当用户点击导航树中的某个条目时,整个树都不会刷新目标页面(导致用户当前节点崩溃)。关于如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:0)

一种方法是将导航树放在一个单独的框架(而不是iframe)中,这会带来自身的问题;由于包含在服务器端发生,所包含内容中导致页面刷新的任何内容都将刷新整个页面,而不考虑通过SSI包含的内容和不包含的内容。使用自己的框架中的树,并将其链接修改为指向内容框架,您将不会遇到此问题 - 当然,使用框架来解决此问题只会给您带来另一个问题。

稍微好一点的选择是使用cookie和Javascript来记录树的哪些节点被扩展。您可以将单击处理程序绑定到每个节点扩展器/控制器,这将更新表示树状态的Javascript对象,并在每次单击事件时,将该对象展平为字符串并通过document.cookie存储它。这样,您可以将树状态持久保存到下一页加载中,其中onload处理程序可以扩展cookie中列出的节点,以使树返回到单击页面重新加载链接时的状态。

这样的事情可能有用,假设存在jQuery(具有本机JSON支持的浏览器或加载的等效库),您的节点扩展器/控制器控件实现为< a>带有“node-control”类的标签,扩展节点的控件具有“扩展”类(可能由预先存在的点击处理程序给出),并且每个都具有唯一标识其在树中的位置的id属性。

 // begin "persistent-node-state.js"

 window.nodesExpanded = {};

 // do the following on document load completion: 
 jQuery(document).ready(function() {
   // check for a nodesExpanded value in the cookie
   var cookieMatch = document.cookie.match(/nodesExpanded=(.*?)\;/);
   if (cookieMatch) { // no nodesExpanded cookie found -- do nothing
     // we found a nodesExpanded cookie; let's unserialize its value into
     // window.nodesExpanded
     window.nodesExpanded = JSON.parse(cookieMatch[1]);
   };

   // iterate through the nodesExpanded object's keys, which are IDs of controls whose
   // nodes we want to expand (if we didn't find a cookie, then window.nodesExpanded 
   // is an empty object, making this loop a very complicated no-op)
   for (node_id in nodesExpanded) {
     // call the pre-existing click handler to expand the node
     jQuery('a.node_control#' + node_id).click();
   };

   // now that that's done, give each node control an additional click handler which 
   // puts its state into window.nodesExpanded and updates the cookie
   jQuery('a.node-control').click(function(i,el) {
     var node_id = jQuery(el).attr('id');

     // if this control's node is expanded...
     if (jQuery(el).hasClass('expanded')) {
       // ...then add the control's id as a key in window.nodesExpanded...
       window.nodesExpanded[node_id] = true;
     }
     else {
       // ...otherwise remove any existing key in window.nodesExpanded with this 
       // node's ID
       window.nodesExpanded[node_id] = undefined;
     };

     // store the updated state in the cookie so it'll persist across page loads
     document.cookie('nodesExpanded=' + JSON.stringify(window.nodesExpanded));
   });
 });

 // end "persistent-node-state.js"

当然,所有这一切都可以在没有jQuery的情况下完成,但加载该库并不需要花费太多 - 你甚至不必下载它,但可以load it from Google's CDN - 它会使除了你需要支持非常旧的(例如IE6之前的版本)浏览器或者那些一般性的东西之外,没有很好的理由不这样做,所以Javascript的所有操作都要容易得多。

类似地,所有各种提示(即,如何识别节点扩展/合同控制,如何判断它是否已扩展,如何触发导致节点扩展或收缩的事件,& c。)根据需要进行修改;在没有站点链接或示例代码的情况下,很难根据您的情况定制示例,所以我尽可能地提供了尽可能通用的内容,同时仍然展示了基本概念,并且不应该太难以编辑。

答案 1 :(得分:0)

服务器端包含就是服务器端。如果您在多个页面中包含相同的包含文件,则每次都会包含该文件。 Apache(或IIS)会看到这一点。另一方面,如果在构建单个“页面”时多个包含文件中包含相同的文件,则可以在包含的文件中设置变量,并在文件包含的每个位置对其进行测试。我这样做的配置文件为我的网站设置了全局变量。

相关问题