Reputation: 545
I have this JS:
<script type="text/javascript">
var aaascript = document.createElement('script'); aaascript.type = 'text/javascript';
aaascript.src = ('https:' == document.location.protocol ? 'https://xxx' : 'http://xxx') + '/aaa.js';
var aaas = document.getElementsByTagName('script')[0]; aaas.parentNode.insertBefore(aaascript,aaas);
callthis('somevalue');
</script>
this code generates a script tag and inserts it to the page. in the script aaa.js is the function callthis. but when I call the function there is this error:
Uncaught ReferenceError: callthis is not defined (anonymous function)
what goes wrong here?
Upvotes: 0
Views: 780
Reputation: 1698
The script
tag is being created but the script is then loaded from the server. callthis()
is being called in between these two events; that is, before the script is fully loaded, and so the method doesn't exist.
Use the .onload
event of the script tag to delay calling callthis()
until the script is fully loaded, as documented here.
Upvotes: 2