views:

65

answers:

1

This question relates to my previous question which asks about loading a page into a DIV asynchronously using jquery/ajax. It has been resolved and it works like charm :). Now the problem is in the View that is asynchronously loaded to the DIV , I'm having a partial view. The partial view intern contains some javascript. When I load the page using ajax/jquery combination it does not load the javascript portion of the PartialView (i.e ascx). But if I load the page directly by typing the url , it shows the javascript properly! Does anybody know a explanation to this behavior?

thanks in advance

/BB

+2  A: 

Javascript is not executed from content loaded by ajax an call. In order to make this work you will need to externalize javascript that needs to be executed into a separate function which you will invoke in the success callback.

$('#searchResults').load('/admin/users', {}, function() {
    someFunctionThatNeedsToBeExecuted();
});

UPDATE:

To execute the function on dropdown change you could do the following:

$('#searchResults').load('/admin/users', {}, function() {
    $('#someDropDownInsertedByThePartialView').change(function() {
        someFunctionThatNeedsToBeExecuted();
    });
});
Darin Dimitrov
@Darvin - my requirement is to execute the this javascript on some other action (on dropdown change) and not on successful callback. is there any other way to register this javascript after the page is loaded?
Bumble Bee
@Bumble Bee, in this case you could subscribe to the `change` event. See my update.
Darin Dimitrov
@Darvin - My bad! I should have tried that. And it worked. Thanks a ton!
Bumble Bee