Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with loading inline javascript in a PartialView

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

like image 725
Illuminati Avatar asked Oct 26 '22 22:10

Illuminati


1 Answers

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();
    });
});
like image 148
Darin Dimitrov Avatar answered Nov 15 '22 04:11

Darin Dimitrov