When loading an html page via ajax, will script tags be loaded?

后端 未结 5 1996
南笙
南笙 2020-11-27 14:23

When you load an html document using AJAX, what does it do with the nodes inside the HEAD tag: (script,link,style,meta,title) ignore them or load and parse them? And in the

5条回答
  •  感动是毒
    2020-11-27 15:13

    when you load an html document using AJAX, what does it do with the nodes inside the HEAD tag: (script,link,style,meta,title)

    That depends how you do the loading. ajax() (as with the XMLHttpRequest on which it is based) itself just gives you a string. How are you getting that into the document?

    If you write that string to the innerHTML of an element, scripts inside it won't be executed. This is not standardised anywhere but all currently popular browsers behave this way.

    However, if you then insert that element into the document (whether it was already inside the document before or not), it will be executed in many browsers, the first time you do it. In IE, the script will be executed when you directly insert a script element into any element, whether in the document or not.

    This is all very inconsistent and inconvenient, which is why you should avoid AJAX-loading

提交回复
热议问题