Hello,
Situation: I send an ajax request that returns HTML containing elements needing event handlers to be set on them. The code that sets the handlers for these elements is contained in a separate javascript file.
I have been using the following code to load the required js files on callback by scripting the <head
> tag. I have not had problems so far, but was wondering if this is a safe and reliable approach (especially cross-browser).
function ajax_callback(response) {
document.getElementById('dom_id_to_update').innerHTML = response;
import_js('/path/to/js/file/');
}
function import_js(src) {
var scriptElem = document.createElement('script');
scriptElem.setAttribute('src',src);
scriptElem.setAttribute('type','text/javascript');
document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
Thanks, Brian