Why is jQuery.load () run twice?

I am using jQuery 1.4 with jQuery history and trying to understand why Firebug / Web Inspector shows 2 XHR GET requests for each page load (twice as much if you visit my websiteโ€™s homepage ( /or /#)).

eg. Visit this (or any) page with Firebug enabled.

Here's the edited / corresponding 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 standard / standard .htaccess
  • I redirect /links-like/thisto /#links-like/thisonly using JavaScript (PE)
    • I reach higher with window.location.replace(addr);, notwindow.location=addr;
  • , .

.

+3
2

, , , ...

.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 ( /).

.

+1

, :

" /links-like/this /#links-like/this JavaScript (PE)"

+2

Source: https://habr.com/ru/post/1736771/


All Articles