How to add DOM element script to head section?

前端 未结 11 1086
野趣味
野趣味 2020-11-28 10:54

I want to add DOM element to head section of HTML. jQuery does not allow adding DOM element script to the head section and they execute instead, Reference.

I want t

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 11:02

    you could do:

    var scriptTag = document.createElement("script");
    scriptTag.type = "text/javascript";
    scriptTag.src = "script_source_here";
    (document.getElementsByTagName("head")[0] || document.documentElement ).appendChild(scriptTag);
    

提交回复
热议问题