Loading javascripts in ajax callbacks -
situation: send ajax request returns html containing elements needing event handlers set on them. code sets handlers these elements contained in separate javascript file.
i have been using following code load required js files on callback scripting <head
> tag. have not had problems far, wondering if safe , 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
yup. can remove script
element after adding (though may want keep around avoid re-loading later — e.g., looking @ script
tags in head
); apparently act of adding that's required code loaded , parsed. more here (that page prototype unofficial wiki, it's applicable regardless of whether you're using prototype).
Comments
Post a Comment