innerhtml中的javascript标签

时间:2013-10-31 11:27:19

标签: javascript html

就像标题所说我试图将javascript标签添加到innerHTML中。 我有一个组合框,根据选择的值,它应该将一个脚本加载到div中 脚本如下所示:

<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>

当我将它放入我的脚本时,它会在我的页面上显示所有类型的文本,这些文本是组合框脚本的一部分。所以我希望有人可以帮助我如何将这些代码插入到我的脚本中,这样它就会显示上面给出的代码加载的内容。

2 个答案:

答案 0 :(得分:1)

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);

       }
    }

答案 1 :(得分:0)

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

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