Can I add javascript dynamically to an existing script element

后端 未结 3 902
无人共我
无人共我 2020-12-06 00:54

I want to dynamically add javascript to an existing script element something like:

var se = document.createElement(\'script\');
se.setAttribute(\'type\', \'t         


        
3条回答
  •  感情败类
    2020-12-06 01:37

    That's not adding JavaScript to an existing script element, it's creating a new script element and adding it to the document.

    This does work in modern browsers, but you wouldn't normally do it unless you had some code in a variable that you really needed to execute in global context (so you couldn't use new Function(), or eval from inside a function).

    What's the use case? Do you really have to do this?

    If you did try to change the script's content by writing to the text content of a

提交回复
热议问题