3

我正在使用带有 jQ​​uery History 的 jQuery 1.4,并试图弄清楚为什么 Firebug/Web Inspector 在每个页面加载时显示 2 个 XHR GET 请求(访问我的网站主页(//#)时该数量翻倍。

例如,在启用 Firebug 的情况下访问(或任何)页面。

这是已编辑/相关的代码(请参阅完整源代码):-

$(document).ready(function() {

    $('body').delegate('a', 'click', function(e) {
        var hash = this.href; 
        if (hash.indexOf(window.location.hostname) > 0) { /* Internal */
         hash = hash.substr((window.location.protocol+'//'+window.location.host+'/').length); 
         $.historyLoad(hash); return false;     
  } else if (hash.indexOf(window.location.hostname) == -1) { /* External */ 
   window.open(hash); return false; 
  } else { /* Nothing to do */ }
        });


 $.historyInit(function(hash) {
  $('#loading').remove(); $('#container').append('<span id="loading">Loading...</span>'); 
   $('#ajax').animate({height: 'hide'}, 'fast', 'swing', function() { 
    $('#page').empty(); $('#loading').fadeIn('fast');

    if (hash == '') { /* Index */ 
     $('#ajax').load('/ #ajax','', function() { ajaxLoad(); }); 
    } else {
        $('#ajax').load(hash + ' #ajax', '', function(responseText, textStatus, XMLHttpRequest) {
      switch (XMLHttpRequest.status) { 
       case 200: ajaxLoad(); break;
       case 404: $('#ajax').load('/404 #ajax','', ajaxLoad); break; // Default 404
       default: alert('We\'re experiencing technical difficulties. Try refreshing.'); break;
       }
      });
     }

}); // $('#ajax')
  }); // historyInit()


  function ajaxLoad() {
   $('#loading').fadeOut('fast', function() { 
    $(this).remove(); $('#ajax').animate({height: 'show', opacity: '1'}, 'fast', 'swing');
    });
   }

    });

一些可能有用的注意事项:-

  • 使用带有默认/标准 .htaccess 的 WordPress
  • 我仅通过 JavaScript 重定向/links-like/this/#links-like/thisPE)
    • window.location.replace(addr);我正在实现上述目标window.location=addr;
  • 如果需要,请随时访问我的网站。

提前致谢。

4

2 回答 2

2

你已经回答了你自己的问题:

“我仅通过 JavaScript (PE)重定向/links-like/this到”/#links-like/this

于 2010-03-13T09:41:53.683 回答
1

我最初在上面发布的代码示例可能有助于回答我自己的问题......

在我的实时站点上,.load() 嵌套在 2 级回调中:-

$.historyInit(function(hash) {
    $('html, body').animate({scrollTop: '0'}, 500, 'swing', function() { \\ level 1
        $('#loading').remove(); $('#container').append('<span id="loading">Loading...</span>'); 
        $('#ajax').animate({height: 'hide'}, 'fast', 'swing', function() { \\ level 2
            $('#page').empty(); $('#loading').fadeIn('fast');

            if (hash == '') { /* Index */ 
                $('#ajax').load('/ #ajax','', function() { ajaxLoad(); }); 
            } else {
                $('#ajax').load(hash + ' #ajax', '', function(responseText, textStatus, XMLHttpRequest) {
                    switch (XMLHttpRequest.status) { 
                        case 200: ajaxLoad(); break;
                        case 404: $('#ajax').load('/404 #ajax','', ajaxLoad); break; // Default 404
                        default: alert('We\'re experiencing technical difficulties. Try refreshing.'); break;
                        }
                    });
                }

            }); // $('#ajax')
        }); // $('html, body')
    }); // historyInit()

...将if (hash)语句移到回调之外使我回到所有页面的 1 XHR GET (/唯一的例外)。

再次感谢您尝试帮助保罗。

于 2010-03-14T21:28:28.960 回答