通过脚本标签加载外部javascript [英] Load external javascript through script tag
问题描述
我正在尝试使用脚本标签动态加载脚本.但我做不到.
我的渲染方法如下
render() {<div><script type="text/javascript" language="javascript" src="//verify.authorize.net/anetseal/seal.js" ></script><a href="http://www.authorize.net/" id="AuthorizeNetText" target="_blank">工作.Yipee</a>
}
我尝试使用危险的SetInnerHtml 但这也不起作用.
我需要这个,因为提到的脚本使用了 document.writeln.所以我希望它显示在当前的 div 中.我无法控制脚本.它来自第三方.
我相信您可以更具体地了解加载脚本后应该做什么.像这样的东西应该很适合你:
componentDidMount() {var aScript = document.createElement('script');aScript.type = 'text/javascript';aScript.src = "/*URL 在这里*/";document.head.appendChild(aScript);aScript.onload = function() {document.getElementById("/*DIV ID HERE*/").InnerHTML =/*您的代码*/;};}
I am trying load a script dynamically using script tag. But I am not able to do it.
my render method is as below
render() {
<div>
<script type="text/javascript" language="javascript" src="//verify.authorize.net/anetseal/seal.js" ></script>
<a href="http://www.authorize.net/" id="AuthorizeNetText" target="_blank">Working. Yipee</a>
</div>
}
I tried to use dangerouslySetInnerHtml but that is also not working.
I require this because, the script that is mentioned uses document.writeln. So I want it to display in this current div. I have no control over the script. It come from a third party.
I believe you could be more specific on what should be done once the script has been loaded. Something like this should suit you well:
componentDidMount() {
var aScript = document.createElement('script');
aScript.type = 'text/javascript';
aScript.src = " /*URL Goes Here*/ ";
document.head.appendChild(aScript);
aScript.onload = function() {
document.getElementById(" /*DIV ID HERE*/ ").InnerHTML = /*YOUR CODE*/;
};
}
这篇关于通过脚本标签加载外部javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!