On May 15, 12:44 am, jk <[EMAIL PROTECTED]> wrote:
> JQuery.load can receive a callback function as one of its parameters.
> According to the online docs, this function is called "when theajax
> request is complete". In practice, it appears this callback is
> performed after theajaxrequest is complete and after the dom is
> inserted, but before new javascript in the HTML isevaluated. Is there
> a way to set a callback for after the javascript isevaluated?

I had to handle this just yesterday; I ended up using setTimeout() to
make it work.

In my case, I have a login form view that is intended to be as
portable as possible.  It allows the including page to perform its own
actions after the login is successfully completed.  To accomplish
this, it executes a loginComplete() method that is supposed to be set
by its parent page.  My quandary as it relates to you is that I can't
override loginComplete() until it exists!

So, loginform.js has a LoginForm namespace with the loginComplete()
function:

var LoginForm = {       //page namespace
        username: function() {
                return $('#loginform_user').val();
        },
        loginComplete: function() { //override by parent page
                alert('login OK');
        }
}
// this should be moved into the LoginForm namespace...
function loginSuccess(responseText, statusText)  {
        if (responseText == 'OK') {
                LoginForm.loginComplete();
        }
}

----

While main.js loads and overrides the login completion method:

//$div = jQuery(div where the login form resides);
$div.load('auth/loginform', {}, function(){
        //once the loginform's scripts have been evaluated,
        //set its event listener
        (function setlistener() {
                if (typeof LoginForm == 'undefined') {
                        //recurse until LoginForm is available, i.e. script 
evaluated
                        setTimeout(setlistener, 10);    //wait 10 ms before 
trying again
                } else {
                        LoginForm.loginComplete = function() {
                                $main.before($loaddiv); //show loading spinner
                                $div.hide('slow');      //hide login form
                                $main.load('http://localhost/wdm/home/wdmhome', 
{}, function() {
                                        $loaddiv.remove();
                                });
                        }
                }
        })();   //immediately execute setlistener()
});

---

I edited out a bunch of additional text; hopefully the gist remains
and is useful.

Pyro

Reply via email to