Having ajaxComplete be specific to one request

I have .ajaxComplete tied to a link click event and I need it to fire only once.

$(document).delegate('.links', 'click', function(evt) {
    evt.preventDefault();
    var origanalUrl = $(this).attr('href');
    var hrefRegEx = /^(^\/?{[A-Za-z\_]*\/?)?([A-Za-z\_]+)(\.php)$/
    var hashUrl2 = origanalUrl.replace(hrefRegEx, '$2');
    var hashUrl = '#' + hashUrl2;
        var urlExt = ' #ajaxTarget';
        var newUrl = origanalUrl + urlExt;
        if(!$(hashUrl).html() && hashUrl != '#index') {
            $(hashUrl).load(newUrl).ajaxComplete(function(event, XMLHttpRequest, ajaxOptions) {
                newMain = $(hashUrl);
                newHeight = newMain.outerHeight();
                alert(newHeight);
                $('#textBox_main').animate({
                    height: newHeight
                    }, 500, function() {
                    newMain.fadeIn(500);
                });
                oldMain = newMain;
            });
        } else {
            isLoaded = true;
        }

The first click is fine, but each additional click stacks on the next ajaxComplete call. How do I prevent this?

Answers


You were attaching a callback to the 'ajaxComplete' event of element $(hashUrl). (The second time you click it, another handler was attached, and so on)

Try passing that callback function into load. Like this:

$(hashUrl).load(newUrl, function(response, status, xhr) {...} );

Need Your Help

Google Places AutoComplete + Android Fragment

android android-fragments autocomplete google-api-client android-googleapiclient

I am a beginner on Android development and I came into a problem which I hope you can help me to solve. I'll keep it simple...i am trying to use Google Places autocompletion in a fragment...the app...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.