jquery load() strips script tags - workaround?

后端 未结 6 395
孤街浪徒
孤街浪徒 2020-11-29 12:18

Does anyone know of a work around for jquery .load() stripping out the script tags loaded from external content?

There\'s a lot of documentation of the fact that thi

6条回答
  •  失恋的感觉
    2020-11-29 12:57

    Update now, which you can find on the jQuery doc site:

    http://api.jquery.com/load/

    It DOES strip tags only if you call load with a specific suffixed selector..

    Script Execution

    When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed. An example of both cases can be seen below:

    Here, any JavaScript loaded into #a as a part of the document will successfully execute. 1

    $('#a').load('article.html');
    

    However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed: 1

    $('#b').load('article.html #target');
    

提交回复
热议问题