Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery + AJAX + double calls

Tags:

jquery

ajax

I am building an ajax based navigation and everything works fine, except one thing:

When i look in Firebug I can see that on each click the ajax site gets called and loaded twice.

This is the script:

$(document).ready(function () {

    //Check if url hash value exists (for bookmark)
    $.history.init(pageload);   

    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //for back button
        $.history.load(hash);   

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the main and show the progress bar
        $('#main').hide();
        $('#loading').show();


        //run the ajax
        getPage();

        //cancel the anchor tag behaviour
        return false;
    }); 
});

//AJAX Navigation Helper function
function pageload(hash) {

    //if hash value exists, run the ajax
    if (hash) getPage();    
}

//AJAX Navigation Helper function   
function getPage() {

    //generate the parameter for the php script
    var data = 'page=' + encodeURIComponent(document.location.hash);

    $.ajax({
        url: "loader.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //hide the progress bar
            $('#loading').hide();   

            //add the main retrieved from ajax and put it in the #main div
            $('#main').html(html);

            //display the body with fadeIn transition
            $('#main').fadeIn('slow');      

            //reload site scripts
            $.getScript("js/script.js"); 

        }       
    });

}

So whenever I click on a list in my navigation, the page gets loaded just fine into #main, but it happens twice (as displayed in Firebug). Any1 maybe has an idea why? :)

I noticed that it doesnt happen when i access a certain page by url (than it only does one ajax call), so I guess the problem is somewhere in the click function.

p.s. i am using the jquery history plugin, but I dont think the problem is there, or?

like image 392
r0skar Avatar asked Dec 22 '22 08:12

r0skar


1 Answers

I'm going to guess that getPage is called twice. Put a debugger statement at the beginning of the function (debugger;) and open firebug to see if it is. Here's one way to fix the problem if it is getting called twice:

//AJAX Navigation Helper function   
function getPage() {

    if(getPage.isLoaded != true) {

        //generate the parameter for the php script
        var data = 'page=' + encodeURIComponent(document.location.hash);

        $.ajax({
            url: "loader.php",  
            type: "GET",        
            data: data,     
            cache: false,
            success: function (html) {  

                //hide the progress bar
                $('#loading').hide();   

                //add the main retrieved from ajax and put it in the #main div
                $('#main').html(html);

                //display the body with fadeIn transition
                $('#main').fadeIn('slow');      

                //reload site scripts
                $.getScript("js/script.js"); 

            }       
        });

    }

    getPage.isLoaded = true;

}
like image 197
Ryan Wright Avatar answered Jan 03 '23 17:01

Ryan Wright