How to Force Javascript to Execute within HTML Response to Ajax Request How to Force Javascript to Execute within HTML Response to Ajax Request ajax ajax

How to Force Javascript to Execute within HTML Response to Ajax Request


The parameter is:

evalScripts:true

Note that you should be using Ajax.Updater, not Ajax.Request

See: http://www.prototypejs.org/api/ajax/updater

Ajax.Request will only process JavaScript if the response headers are:

application/ecmascript, application/javascript, application/x-ecmascript, application/x-javascript, text/ecmascript, text/javascript, text/x-ecmascript, or text/x-javascript

Whereas Ajax.Updater will process JS is evalScripts:true is set. Ajax.Request is geared toward data transport, such as getting a JSON response.

Since you are updating HTML you should be using Ajax.Updater anyways.


Does setting evalScripts: true as an option help?


You should be able to do something like this:

div.innerHTML = "<div onclick='someOtherFunctionTocall();'>";

If you need to execute something at the same time as injecting the HTML, can you modify the signature of ajaxModify() by passing another parameter, which will be the javascript function you're going to execute (if it's not null - which let's you keep it optional, as you surely won't want to execute something on EVERY AJAX response).