This isn't pretty, but it works:
<script type="text/javascript">
document.write('<script type="text/javascript" src="other.js"></script>');
</script>
<script type="text/javascript">
functionFromOther();
</script>
Or
<script type="text/javascript">
document.write('<script type="text/javascript" src="other.js"></script>');
window.onload = function() {
functionFromOther();
};
</script>
The script must be included either in a separate <script>
tag or before window.onload()
.
This will not work:
<script type="text/javascript">
document.write('<script type="text/javascript" src="other.js"></script>');
functionFromOther(); // Error
</script>
The same can be done with creating a node, as Pointy did, but only in FF. You have no guarantee when the script will be ready in other browsers.
Being an XML Purist I really hate this. But it does work predictably. You could easily wrap those ugly document.write()
s so you don't have to look at them. You could even do tests and create a node and append it then fall back on document.write()
.