7

I have jQuery .load() function like in load_to.html page

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

However, this doesn't seems to be loading javascript from load_from.html page. Is there any way, I can load javascript.

2 Answers 2

8

how about using .getScript()

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

Sign up to request clarification or add additional context in comments.

Comments

6

From jQuery's documentation for .load():

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

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

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

Perhaps you can request a list of scripts to load from the server with ajax:

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

6 Comments

what would happen if the script tags were moved inside the #bodyPart element?
hmm, interesting... looking at the unminified source, the load function strips any script tag to prevent security errors in IE (// inject the contents of the document in, removing the scripts // to avoid any 'Permission Denied' errors in IE) (line 6240)
I have some inline scripts embded in load_from.html file.
@Krishna, I know - but unfortunately you can't load those in this way. You will need to load those inline scripts separately using the method I have describe, or .getScript() as suggested by dane.
Thanks for @Ben, @Box9. Btw I am loading fragmentation of by $('#targetID').load('/load_from.html #bodyPart, script'). however his seems inclding <div id="bodyPart">. Is there any way to exclude <div id="bodyPart"> from load_from.html page
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.