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.
The HTML <script> tag is used for declaring a script within your HTML document. Through this, you can define client-side JavaScript. But, what if you want to add external JavaScript inside an HTML Page? Well, you can easily do that too using the src attribute of the <script> tag. The following are the attributes of the <script> tag −
script.innerHTML = 'alert ("Inline script loaded!")' As you can see, it’s easy to create and append new script elements, allowing us to include any number of external JavaScript files dynamically after a page has loaded. The real challenge isn’t loading the file – it’s knowing when the file has finished loading.
There are two ways you can use the script tag to insert JavaScript in HTML. You can insert JavaScript directly into an HTML file. Here is an example of how you would do that using the script tag. I will go ahead and start with the script tag. Between the script tag, I will create a function that prints the text “Hello World” to the console.
Here’s an inline example: script.innerHTML = 'alert ("Inline script loaded!")' As you can see, it’s easy to create and append new script elements, allowing us to include any number of external JavaScript files dynamically after a page has loaded.
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*/;
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With