jquery烧烤不能得到漂亮的网址;使用pushstate

时间:2011-04-05 15:54:18

标签: jquery ajax string url

谢谢。

网址看起来像这样: http://mysite.com/#main=mysite.php%3Fid%3D108&main=mysite.php%3Fid%3D108

我试图让它们看起来像这样:  http://mysite.com/#main=mysite.php?id=108&main=mysite.php?id=108

以下是代码:

  $(function(){
      $('.bbq').each(function(){
        $(this).data( 'bbq', {
          cache: {
            '': $(this).find('.bbq-default')
          }
        });
      });

  // For all links inside a .bbq widget, push the appropriate state onto the
  // history when clicked.
      $('.bbq a[href^=#]').live( 'click', function(e){
        var state = {},

          // Get the id of this .bbq widget.
          id = $(this).closest( '.bbq' ).attr( 'id' ),

          // Get the url from the link's href attribute, stripping any leading #.
          url = $(this).attr( 'href' ).replace( /^#/, '' );

        // Set the state!
        state[ id ] = url;
        $.bbq.pushState( state );

        return false;
      });

  $(window).bind( 'hashchange', function(e) {

    // Iterate over all .bbq widgets.
      var that = $(this),

        // Get the stored data for this .bbq widget.
        data = that.data( 'bbq' ),

        // Get the url for this .bbq widget from the hash, based on the
        // appropriate id property. In jQuery 1.4, you should use e.getState()
        // instead of $.bbq.getState().
        url = $.bbq.getState( that.attr( 'id' ) ) || '';

      // If the url hasn't changed, do nothing and skip to the next .bbq widget.
      if ( data.url === url ) { return; }

      // Store the url for the next time around.
      data.url = url;

      // Remove .bbq-current class from any previously "current" link(s).
      that.find( 'a.bbq-current' ).removeClass( 'bbq-current' );

      // Hide any visible ajax content.
      that.find( '.bbq-content' ).children( ':visible' ).hide();

      // Add .bbq-current class to "current" nav link(s), only if url isn't empty.
      url && that.find( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );

      if ( data.cache[ url ] ) {
        // Since the widget is already in the cache, it doesn't need to be
        // created, so instead of creating it again, let's just show it!
        data.cache[ url ].show();

      } else {
        // Show "loading" content while AJAX content loads.
        that.find( '.bbq-loading' ).show();

        // Create container for this url's content and store a reference to it in
        // the cache.
        data.cache[ url ] = $( '<div class="bbq-item"/>' )

          // Append the content container to the parent container.
          .appendTo( that.find( '.bbq-content' ) )

          // Load external content via AJAX. Note that in order to keep this
          // example streamlined, only the content in .infobox is shown. You'll
          // want to change this based on your needs.
          .load( url, function(){
            // Content loaded, hide "loading" content.
            that.find( '.bbq-loading' ).hide();
          });
      }
    });
  })

            $(window).trigger( 'hashchange' );

});

修改

我应该说得更清楚: http://mysite.com/#main=page1.php?id=108&main2=page2.php?id=108 用字符串加载其他页面。

1 个答案:

答案 0 :(得分:1)

您要求提供漂亮的网址,但您要求的网址格式远非“漂亮”。

您要求的网址如下所示:

http://mysite.com/#main=page1.php?id=108&main2=page2.php?id=108

基本上,您的网址格式告诉我您所在的网页上引用了自己网址中的其他网址,而其引用的网址本身就是引用第三个网址。这是一个非常复杂的URL方案,可能会使您的用户感到困惑。你想用它实现的是什么?

以这种方式将组件添加到ajax站点中的URL哈希的目的是为用户提供他可以链接或书签的内容。问问自己,你给出的参数是否会对此有所帮助。

在我看来,您只需在URL哈希中使用id=108即可实现相同的功能。其他所有内容都是用户不应该在URL中看到的漏洞。您的Javascript代码可以在内部重建ajax URL,而无需用户了解它们。