javascript-为什么jQuery .load()触发两次?

前端之家收集整理的这篇文章主要介绍了javascript-为什么jQuery .load()触发两次? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在将jQuery 1.4与jQuery History结合使用,并试图弄清为什么Firebug / Web Inspector在每个页面加载时显示2个XHR GET请求(访问我的网站首页(/或/#)时,该请求数量是原来的两倍).

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

这是已编辑/相关的代码(请参见full source):-

  1. $(document).ready(function() {
  2. $('body').delegate('a','click',function(e) {
  3. var hash = this.href;
  4. if (hash.indexOf(window.location.hostname) > 0) { /* Internal */
  5. hash = hash.substr((window.location.protocol+'//'+window.location.host+'/').length);
  6. $.historyLoad(hash); return false;
  7. } else if (hash.indexOf(window.location.hostname) == -1) { /* External */
  8. window.open(hash); return false;
  9. } else { /* Nothing to do */ }
  10. });
  11. $.historyInit(function(hash) {
  12. $('#loading').remove(); $('#container').append('<span id="loading">Loading...</span>');
  13. $('#ajax').animate({height: 'hide'},'fast','swing',function() {
  14. $('#page').empty(); $('#loading').fadeIn('fast');
  15. if (hash == '') { /* Index */
  16. $('#ajax').load('/ #ajax','',function() { ajaxLoad(); });
  17. } else {
  18. $('#ajax').load(hash + ' #ajax',function(responseText,textStatus,XMLHttpRequest) {
  19. switch (XMLHttpRequest.status) {
  20. case 200: ajaxLoad(); break;
  21. case 404: $('#ajax').load('/404 #ajax',ajaxLoad); break; // Default 404
  22. default: alert('We\'re experiencing technical difficulties. Try refreshing.'); break;
  23. }
  24. });
  25. }
  26. }); // $('#ajax')
  27. }); // historyInit()
  28. function ajaxLoad() {
  29. $('#loading').fadeOut('fast',function() {
  30. $(this).remove(); $('#ajax').animate({height: 'show',opacity: '1'},'swing');
  31. });
  32. }
  33. });

一些注意事项可能会有所帮助:-

>在默认/标准.htaccess中使用wordpress
>我仅通过JavaScript(PE)将/ links-like / this重定向到/#links-like / this

>我通过window.location.replace(addr);实现了上述目标而不是window.location = addr;

>如果需要,请随时访问my site.

提前致谢.

最佳答案
我认为您已经回答了自己的问题:

“I’m redirecting /links-like/this to /#links-like/this via JavaScript only (PE)”

猜你在找的jQuery相关文章