简体   繁体   中英

javascript tag in innerhtml

Like the title says I'm trying to add a javascript tag into a innerHTML. I have a combobox and depending on which value is selected it should load a script into a div the script looks like this:

<script type="text/javascript">
_cashieProductID=292016;
document.write(unescape("%3Cscript src='" + 
('https:' == document.location.protocol ? 'https://' : 'http://') + 
"somelink.js' 
type='text/javascript'%3E%3C/script%3E"));
</script>

when I put this into my script it shows all kind of text on my page that is part of the combobox script. So I hope someone can help me how to insert this code into my script so it will show the content that gets loaded with the code given above.

HTML

<select id="product" onchange="validate()">

function validate()
{
     pName = document.getElementById('product');
     var value = pName.options[pName.selectedIndex].value;
     if(value == "4OFC")
     loadJs(detail,"myjsSrc");
     else if(/*some conditon*/)
     {
         // use this ladder to change the script address based on value of select box
     }
     else{
     }
}
   function loadJs(targetDiv,source)
    {
       var ele = document.getElementById(targetDiv);
       if(ele)
       {
          var scr = document.createElement('script');
          scr.type = 'text/javascript';
          scr.src = source;
          ele.appendChild(scr);

       }
    }

只需在document.write中尝试实际的字符串,如下所示:

document.write("<script src='" + ('https:' == document.location.protocol ? 'https://' : 'http://') + "somelink.js' type='text/javascript'></script>");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM