dtjpnd7517 2011-09-21 17:33
浏览 39
已采纳

jquery地址爬行 - 逻辑问题

I'm using the jquery address plugin to build an ajax driven site, and i've got it working! Yay! For the purposes of this question we can use the test site:

http://www.asual.com/jquery/address/samples/crawling
http://www.asual.com/download/jquery/address
(I had to remove two calls to urlencode() to make the crawling example work.)

I'm encountering a problem with the $crawling->nav() call. It basically uses js and php to load parts of an xml file into the dom. I (mostly) understand how it works, and I would like to modify the example code to include sub pages.

For example, I would like to show 'subnav-project.html' at '/!#/project' and '/!#/project/blue', but not at '/!#/contact'. To do this, I figure php should 'know' what page the user is on, that way I can base my logic off of that.

Is this crazy? Can php ever know the current state of the site if I'm building it this way? If not, how does one selectively load html snippets, or modify what links are shown in navigation menus?

I've never gotten too crazy with ajax before, so any feedback at all would be helpful.

EDIT

This is the crawling class.

class Crawling { 

    const fragment = '_escaped_fragment_';

    function Crawling(){ 

        // Initializes the fragment value
        $fragment = (!isset($_REQUEST[self::fragment]) || $_REQUEST[self::fragment] == '') ? '/' : $_REQUEST[self::fragment];

        // Parses parameters if any
        $this->parameters = array();
        $arr = explode('?', $fragment);
        if (count($arr) > 1) {
            parse_str($arr[1], $this->parameters);
        }

        // Adds support for both /name and /?page=name
        if (isset($this->parameters['page'])) {
            $this->page = '/?page=' . $this->parameters['page'];
        } else {
            $this->page = $arr[0];
        }

        // Loads the data file
        $this->doc = new DOMDocument();
        $this->doc->load('data.xml');
        $this->xp = new DOMXPath($this->doc);
        $this->nodes = $this->xp->query('/data/page');
        $this->node = $this->xp->query('/data/page[@href="' . $this->page . '"]')->item(0);

        if (!isset($this->node)) {
            header("HTTP/1.0 404 Not Found");
        }
    }

    function base() {
        $arr = explode('?', $_SERVER['REQUEST_URI']);
        return $arr[0] != '/' ? preg_replace('/\/$/', '', $arr[0]) : $arr[0];
    }

    function title() {
        if (isset($this->node)) {
            $title = $this->node->getAttribute('title');
        } else {
            $title = 'Page not found';
        }
        echo($title);
    }

    function nav() {
        $str = '';

        // Prepares the navigation links
        foreach ($this->nodes as $node) {
            $href = $node->getAttribute('href');
            $title = $node->getAttribute('title');
            $str .= '<li><a href="' . $this->base() . ($href == '/' ? '' : '?' . self::fragment . '=' .html_entity_decode($href)) . '"' 
                . ($this->page == $href ? ' class="selected"' : '') . '>' 
                . $title . '</a></li>';
        }
        echo($str);
    }

    function content() {
        $str = '';

        // Prepares the content with support for a simple "More..." link
        if (isset($this->node)) {
            foreach ($this->node->childNodes as $node) {
                if (!isset($this->parameters['more']) && $node->nodeType == XML_COMMENT_NODE && $node->nodeValue == ' page break ') {
                    $str .= '<p><a href="' . $this->page . 
                        (count($this->parameters) == 0 ? '?' : '&') . 'more=true' . '">More...</a></p>';
                    break;
                } else {
                    $str .= $this->doc->saveXML($node);
                }
            }
        } else {
            $str .= '<p>Page not found.</p>';
        }

        echo(preg_replace_callback('/href="(\/[^"]+|\/)"/', array(get_class($this), 'callback'), $str));
    }

    private function callback($m) {
        return 'href="' . ($m[1] == '/' ? $this->base() : ($this->base() . '?' . self::fragment . '=' .$m[1])) . '"';
    }
}

$crawling = new Crawling();
  • 写回答

1条回答 默认 最新

  • duanqiao1961 2011-09-21 17:39
    关注

    You won't be able to make server-side decisions using the fragment-identifier (i.e., everything to the right of the # character). This is because browsers don't send fragment-identifiers to the server. If you're going to want to make server-side decisions, you'll need to use some JavaScript assistance (including AJAX) to communicate what the current fragment-identifier is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来