jQuery .load() doesn't load script

前端 未结 2 653
遇见更好的自我
遇见更好的自我 2021-01-02 11:12

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

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

However,

相关标签:
2条回答
  • 2021-01-02 11:21

    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');
        }
    });
    
    0 讨论(0)
  • 2021-01-02 11:44

    how about using .getScript()

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

    0 讨论(0)
提交回复
热议问题