jquery ajax and html

By : Jing
Source: Stackoverflow.com
Question!

Does anybody know how to execute javascript functions in an html that is loaded via ajax? I mean the html contains both plain text and javascript. In my case it seems that only the in-line javascript (eg. onclick="dosomething();return false") gets executed. The pre-defined functions which are wrapped by < script language = "javascript >are unfortunately ignored..

i have search couple articles through the site but the results are either use getScript to load a pure js or use eval. please help me out of this! Thank you very much!

By : Jing


Answers

Look up jquery event delegation, and the .live() method.

http://docs.jquery.com/Events/live#typefn

Event delegation is especially useful and efficient if you're working with a large piece of html that you've added dynamically.

By : ScottE


I don't know if this fixes your problem... but if your loaded html also includes a link to jquery's code base, it can cause issues with the child-code not correctly linking with the handle to the jquery object ($).

By : Ape-inago


If I understand you correctly, you receive from your AJAX call plain HTML mixed with javascript code, e.g. with tags. I'm didn't checked it, but you can try to do the following:

$.get( ajaxURL, function( data)
{
   $( "head").append( $( data).find( "script"));
   yourFunctionCall( );
});


This video can help you solving your question :)
By: admin