jquery地址爬行 - 逻辑问题

时间:2011-09-21 17:33:35

标签: php jquery ajax jquery-address

我正在使用jquery地址插件来构建一个ajax驱动的网站,我已经开始工作了!好极了!出于这个问题的目的,我们可以使用测试站点:

http://www.asual.com/jquery/address/samples/crawling
http://www.asual.com/download/jquery/address
(我必须删除对urlencode()的两次调用才能使爬网示例正常工作。)

我遇到了$crawling->nav()电话的问题。它基本上使用js和php将部分xml文件加载到dom中。我(大多数)理解它是如何工作的,我想修改示例代码以包含子页面。

例如,我想在'/!#/project''/!#/project/blue'展示'subnav-project.html',而不是'/!#/contact'。要做到这一点,我认为php应该“知道”用户所在的页面,这样我就可以将我的逻辑基于此。

这是疯了吗?如果我用这种方式构建它,php能不能知道网站的当前状态?如果没有,如何有选择地加载html片段,或修改导航菜单中显示的链接?

我以前从未对ajax过于疯狂,所以任何反馈都会有所帮助。

修改

这是抓取类。

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 个答案:

答案 0 :(得分:0)

您将无法使用fragment-identifier(即#字符右侧的所有内容)做出服务器端决策。这是因为浏览器不会将片段标识符发送到服务器。如果您想要做出服务器端决策,则需要使用一些JavaScript帮助(包括AJAX)来传达当前片段标识符的内容。