[Solved] jQuery .load() doesn’t load script

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.

Enquirer: Elisa


Solution #1:

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');
Respondent: David Tang

Solution #2:

how about using .getScript()

Respondent: dane

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy