簡體   English   中英

innerhtml中的javascript標簽

[英]javascript tag in innerhtml

就像標題說的那樣,我正在嘗試將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>

當我將其放入腳本時,它會在頁面上顯示所有屬於組合框腳本的文本。 因此,我希望有人能幫助我如何將這段代碼插入到我的腳本中,以便它將顯示上面給出的代碼加載的內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM