Home > Article > Web Front-end > Let innerHTML scripts also run_javascript skills
Recently, I have been thinking about the reconstruction of pjblog. Of course, before reconstruction, I must think clearly about everything that needs to be done. Among them, I thought of a js improvement content that requires innerHTML scripts to be able to run. But I tried it and found out The script directly innerHTML cannot be executed under IE. After several attempts, I found that in the inserted HTML, there are other objects in front of <script>, and the script is set to defer. Such script innerHTML is executed by IE. However, this trick does not work for Firefox. Ling~~ Later I found some information and found that firefox can re-append or insertBefore the object once to execute the script inside. <br><br> Program code<BR>var fillHTML = function (el,HTMLString) { <BR> if (!el) return; <BR>if (window.ActiveXObject) { //For IE <BR>el.innerHTML = "<img style="max-width:90%"/ alt="Let innerHTML scripts also run_javascript skills" >" HTMLString.replace(/<script([^>]*)>/ig, '<script defer>'); <BR>el.removeChild(el.firstChild) <BR>} else { //For Mozilla,Opare <BR>var nSibling = el.nextSibling; <BR>var pNode = el.parentNode; <BR> pNode.removeChild(el); <BR>el.innerHTML = HTMLString; <BR>pNode.insertBefore(el,nSibling) <BR>} <BR>}</script>