<script src="module1.js"></script>
but the problem, as Schwern pointed out, is that if module1.js wants to include module2.js, the user has to include both in HTML-space, which breaks encapsulation.
Sean Burke came up with a solution which addresses the problem by first loading a bootstrapping script, bootstrap.js.
I decided to try a different hack in HTML, by using hidden iframes. First, a disclamer: this was a quick hack, so it is certainly not perfect, and I wouldn't be surprised if someone invented it before, but I came up with this on my own.
<!-- include module1 -->
<iframe name="module1" src="module1.html" style="display:none"></iframe>
<p onclick="module1.my_func();">module1.my_func()</p>
<p onclick="module1.module2.my_func();">module1.module2.my_func()</p>
Then, in module1.html:
<!-- include module2 -->
<iframe name="module2" src="module2.html" style="display:none"></iframe>
<script type="text/javascript">
function my_func() {
alert("module1.my_func");
module2.my_func();
}
</script>
And in module2.html:
<script type="text/javascript">
function my_func() {
alert("module2.my_func");
}
</script>
To summarize:
JavaScript includes and namespaces -- an iframe hack 0 Comments More | Login | Reply /