I'm trying to switch out some old HTML that uses document.write
to prevent a remote website hanging if script.js
doesn't load right away. I have access to script.js
to change it.
My example code:
<script language="JavaScript">
var foo1 = "test 1";
var foo2 = "test 2";
document.write('<script src="script.js?id=' + foo1 + '&num=' + foo2 + '"><\/script>');
</script>
Example of script.js
output.
document.write("<a href='http://example.com/file.php?id=123' title='title'>some data</a>")
How would I go about making my above code load asynchronously?
Update:
I found a snippet of code that I believe will do the trick.
<script>
var script = document.createElement('script');
script.src = "script.js";
document.getElementsByTagName('head')[0].appendChild(script);
</script>
I just don't understand what to put in script.js
to make the snippet of code above work with my output example earlier. What should be on script.js for the snippet of code above to work?