in a previous SO post a solution was found to the issue of dynamically loading pages with embedded <script>
tags. however, it was not a comprehensive solution, as I've come to find. The scenario that breaks the code (please review previous post and its solution), is something that looks like this:
- svc.html -
<script type="text/javascript" src="/plugin.js" css="/plugin.css"></script>
<script type="text/javascript">
plugin_method();
</script>
<div id='inner'>
dynamically loaded content
</div>
where plugin.js
looks the same as before but contains a function definition for plugin_method
.
The problem seems to be that when the script node is added to the document:
target[0].appendChild(scriptNode);
the code is not executed immediately. If (as in the previous post) there is only one script tag in the svc.html
page, everything is fine, but once there is a second one, the $('script:last')
in the plugin no longer points to the correct script, and therefore fails to load the stylesheet.
furthermore, the body of the second script tag seems to get executed before sourcing-in the code for the first, such that the method call fails (since the function hasn't been defined yet).
I've found a couple of links, which I'm mulling over:
http://requirejs.org/docs/why.html http://unixpapa.com/js/dyna.html
ideas anyone?