So I have a function that writes a string into a div.
The string is declared as content, which consists of html code, like this:
<h2>This is a test result</h2>
<script type="text/javascript" src="example_content.js"></script>
<script type="text/javascript"> document.write(example_content); </script>
where example_content.js :
example_content = '<p>some text some text some text some text some text</p>'
+ '<p>some more text....</p>'
+ '<hr />'
+ '<p>End of text</p>';
Problem is, instead of getting this in the div:
<h2>This is a test result</h2>
<p>some text some text some text some text some text</p>
<p>some more text....</p>
<hr />
<p>End of Text</p>
I end up, literaly, with this:
<h2>This is a test result/h2>
<script type="text/javascript" src="example_content.js"></script>
<script type="text/javascript"> document.write(example_content); </script>
Is there a way to avoid writting the script tag as a string ? but as an actual script ?
Thanks for any help in advance 🙂
You can’t do that. You can’t add script tags and execute them by writing the HTML. If you must load scripts dynamically, you should add them to the DOM. You can try this to load an external script file and execute the method.
What it basically does is that it creates a new script element dynamically and adds it to the DOM. It also sets an
onloadevent handler which would fire once the script is downloaded completely.