views:

117

answers:

1

Ok, this is killing me. I'm loading an external page with jquery:

var response = $.ajax({
    url: "example.php",
    dataType: "html",   
    async: false,
});
$("#content2").empty().prepend(response.responseText);

This part is working perfectly.

Now I know that if I I want to interact with the new content I would have to bind any new elements to an action for it to work...

$(".example").live("click",function(event){
    //do something
}):

BUT, the problem is that the content that I am loading contains several .js files that contains LOTS of form validation.

Is there a way to just bind all of the new content to these external .js files?

+3  A: 

Including JavaScript via AJAX requires those scripts to be eval'd. See $.getScript for a better way to do this.

As far as your example AJAX request is concerned, be wary of making synchronous requests as they will freeze your browser for the duration of the remote request. Instead, use asynchronous requests and handle their responses via callbacks

$.ajax({
    url: "example.php",
    dataType: "html",   
    success: function(response) {
        $("#content2").html(response.responseText);
    }
});
Justin Johnson
Well I was using async: false because this is a multi-step form. The form needs to wait for a response before continuing to decide which action to take. When I try to do this the default way, the if statements aren't run properly (I'm guessing because they are being run before a response is received).Yes, I've tried using $.getScript but I'm not sure at what point I should call the js files - every way I can think of to do so doesn't bind the new content to the external js
Joshua
Could you expose more of your code so I can better help you?
Justin Johnson