Why is jQuery .load() firing twice?

Posted by LeslieOA on Stack Overflow See other posts from Stack Overflow or by LeslieOA
Published on 2010-03-13T09:36:11Z Indexed on 2010/03/13 9:45 UTC
Read the original article Hit count: 581

Hello S-O.

I'm using jQuery 1.4 with jQuery History and trying to figure out why Firebug/Web Inspector are showing 2 XHR GET requests on each page load (double that amount when visiting my sites homepage (/ or /#).

e.g. Visit this (or any) page with Firebug enabled.

Here's the edited/relevant code (see 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');
    });
   }

    });

A few notes that may be helpful: -

  • Using WordPress with default/standard .htaccess
  • I'm redirecting /links-like/this to /#links-like/this via JavaScript only (PE)
    • I'm achieving the above with window.location.replace(addr); and not window.location=addr;
  • Feel free to visit my site if needed.

Thanks in advanced.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ajax