I am working with an MVC framework with PHP and in my views I only try to load whats needed for the view. However sometimes I need something from a script that may not have been called into play with the loading of the DOM. This is mostly out of the sake of curiosity to know if there is a way I can load another javascript file into my DOM dynamically after the DOM has rendered or can it only be pulled in while the DOM is rendering?
5 Answers
The easiest way to do this is $.getScript(); An Example would be:
$(document).ready(function(){
$.getScript('urlToJavaScript.js', function(data, textStatus){
// If urlToJavaScript.js is loaded...
console.log(data); //data returned
console.log(textStatus); //success
console.log('Load was performed.');
});
});
1 Comment
chris
dunno how I ever missed that one in the jQuery Guide, thanks for pointing it out.