J
john.lum
I am using document.write in an external javascript file to display
some content, but I've found that I can't rely on that content being
displayed in the location I expect.
Here's a very simple example:
=====================================================
<!-- test1.htm -->
<html>
<body>
<script language="javascript">
document.write('<script language="javascript"
src="test1.js"><\/script>');
document.write('<p>LINE 2: Should appear below LINE 1, but does
not...</p>');
</script>
</body>
</html>
=====================================================
// test1.js
document.write('<p>LINE 1: Should appear above LINE 2, but does
not...</p>');
=====================================================
What happens here is that LINE 2 is displayed above LINE 1 when the
document is loaded by my browser. I vaguely understand why the
javascript parser might do this, but in the end, I want LINE 1 to show
up before LINE 2, and I'm wondering how to achieve it.
I am aware that I can force the order I want in this particular example
by adding a </script><script> line after the first document.write line.
However, in my real code, I've got primary external js file (say,
test2.js) from which I am bringing in the test1.js content; I'm not
doing it from an HTML file. In this situation, I'm not sure what
syntactical trick I can play to cause the test1.js content to be
rendered at the desired location.
Does anyone have any thoughts about how I might resolve this?
Thanks much,
John
some content, but I've found that I can't rely on that content being
displayed in the location I expect.
Here's a very simple example:
=====================================================
<!-- test1.htm -->
<html>
<body>
<script language="javascript">
document.write('<script language="javascript"
src="test1.js"><\/script>');
document.write('<p>LINE 2: Should appear below LINE 1, but does
not...</p>');
</script>
</body>
</html>
=====================================================
// test1.js
document.write('<p>LINE 1: Should appear above LINE 2, but does
not...</p>');
=====================================================
What happens here is that LINE 2 is displayed above LINE 1 when the
document is loaded by my browser. I vaguely understand why the
javascript parser might do this, but in the end, I want LINE 1 to show
up before LINE 2, and I'm wondering how to achieve it.
I am aware that I can force the order I want in this particular example
by adding a </script><script> line after the first document.write line.
However, in my real code, I've got primary external js file (say,
test2.js) from which I am bringing in the test1.js content; I'm not
doing it from an HTML file. In this situation, I'm not sure what
syntactical trick I can play to cause the test1.js content to be
rendered at the desired location.
Does anyone have any thoughts about how I might resolve this?
Thanks much,
John