2010-03-13 162 views
3

我使用jQuery 1.4与jQuery历史,并试图找出为什么萤火虫/ Web Inspector是显示访问我的网站主页(//#)时,在每个页面加载(双倍金额2个XHR GET请求。为什么jQuery .load()会触发两次?

如访问this(或)使用Firebug页面启用

以下是编辑/相关代码(见full source): - 。

$(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'); 
    }); 
    } 

    }); 

的几个注意事项,可能会有所帮助: -

  • 使用WordPress的默认/标准的.htaccess
  • 我通过只有JavaScript(PE)重定向到/links-like/this/#links-like/this
    • 我实现以上window.location.replace(addr);而不是window.location=addr;
  • 如有需要,请随时访问my site

在此先感谢。

回答

1

我原来上面贴的代码示例可能有助于回答我自己的问题...

在我的生活网站.load()嵌套在回调的两个级别: -

$.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获取所有页(/作为唯一的例外)。

再次感谢您试图帮助Paulo。

2

认为你已经回答了自己的问题: “通过JavaScript只(PE),我重定向到/links-like/this/#links-like/this

+0

不,试过了。我在页面头部以纯JS显示如下: - '' 注释掉'window.location.replace(g);'仍然有同样的效果。还有其他建议吗? 无论如何感谢保罗。 – LeslieOA 2010-03-13 10:36:40

相关问题