Reputation: 4808
Example function:
function myFunction() {
alert("Test");
}
myFunction();
This will run fine when called in a normal HTML webpage. But if this is part of the innerHTML content that is returned from an AJAX call, the function call myFunction();
won't work.
For example, if the content of a div TestDiv
is changed via AJAX, and the innerHTML contains the following:
<span>AJAX Result</span>
<script language="JavaScript">myFunction();</script>
This code will not work when the AJAX request is complete. How can this be fixed?
Upvotes: 0
Views: 72
Reputation: 10736
Call your function in the success method on the ajax request.
$.ajax({
url: 'your url',
data: { data: data},
success: function(data) {
// Your function
myFunction();
}
});
btw if you're using html5, the type is no longer needed on the script tag
<script></script>
otherwise it's
<script type="text/javascript"></script>
Upvotes: 1