Jquery BBQ不能得到漂亮的url;使用pushstate



谢谢。

url是这样的:http://mysite.com/主要= mysite.php % 3 fid % 3 d108&主要= mysite.php % 3 fid % 3 d108

我想让他们看起来像这样:http://mysite.com/主要= mysite.php ? id = 108,主要= 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使用字符串加载其他页面

你要求漂亮的URL,但是你要求的URL格式远远不够"漂亮"。

你想让你的URL看起来像这样:

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

基本上你的URL格式告诉我你在一个页面上它在它自己的URL中引用了另一个URL,它引用的那个URL本身也引用了第三个URL。这是一个非常复杂的URL方案,很可能会让您的用户感到困惑。你想用它达到什么目的?

在ajax站点中以这种方式向URL散列添加组件的目的是为用户提供可以链接或收藏的内容。问问你自己,你给出的参数是否会有所帮助。

在我看来,你可以在你的URL哈希中简单地使用id=108来实现同样的事情。其他内容都是用户不需要在URL中看到的内容。您的Javascript代码可以在内部重新构建ajax url,而无需用户知道它们。

最新更新