search

Home  >  Q&A  >  body text

javascript - There are js imports in the innerHTML content or the statements cannot run properly.

When using innerHTML to add content on the

node, we found that if there is a <script> tag in the innerHTML string, the js statement in the tag cannot be run and the src attribute is invalid (external js files cannot be imported). How to solve this problem?

習慣沉默習慣沉默2694 days ago889

reply all(2)I'll reply

  • typecho

    typecho2017-06-30 10:00:51

    innerHTML can insert link tags, but cannot insert script tags; the insertion of script tags can be completed through appendChild of js.

    var oScript = document.createElement('script'); 
    oScript.type = 'text/javascript'; 
    oScript.async = true; 
    oScript.src = url; 
    document.body.appendChild(oScript); 

    reply
    0
  • 代言

    代言2017-06-30 10:00:51

    The nested HTML document in the body is not written in a standardized way

    reply
    0
  • Cancelreply