JQuery.load () does not load script

I have a jQuery .load() function, as in the page load_to.html

 $('#targetID').load('/load_from.html #bodyPart, script') 

However, this does not seem to be loading javascript from the load_from.html page. Is there any way I can download javascript.

+6
jquery
source share
2 answers

From jQuery documentation for .load() :

jQuery uses the browser .innerHTML property to parse the extracted document and paste it into the current document. During this process, browsers often filter elements from a document, such as <html> , <title> , or <head> .

To load scripts, you must create the <script> elements in the <head> document yourself:

 $('<script>', {src: 'js_file.js'}).appendTo('head'); 

Perhaps you can request a list of scripts to download from the server using ajax:

 $.post('scripts_to_load.json', function (data) { for (var i = 0; i < data.scripts.length; i++) { $('<script>', {src: data.scripts[i]}).appendTo('head'); } }); 
+5
source share

how about using .getScript ()

http://api.jquery.com/jQuery.getScript/

+8
source share

All Articles