473,396 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

External js files, document.write, and (apparent) deferred evaluation

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

Feb 21 '06 #1
6 2655


jo******@gmail.com wrote:

What happens here is that LINE 2 is displayed above LINE 1 when the
document is loaded by my browser.


Which browser exactly is that? And do you only care about that
particular browser or are you looking for consistency across browsers?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 21 '06 #2
> Which browser exactly is that?

Good question, sorry I didn't specify. I observe this behavior in
Netscape 7.02 and IE 6.0 (XP SP2). Those are the browsers that I
really care about.

The underlying situation here is that I've written a UI component that
I want to bring in via javascript from a variety of page types
(straight HTML, PHP, and perl template toolkit templates). There's
already a defined external header.js file that is brought in by most of
these pages, so I figured it was a good idea to use document.write
there to bring in the code associated with my new component.

Now I'm wary, since I encountered these display issues...any advice is
much appreciated.

Thanks,
John

Feb 21 '06 #3
I think that the problem is related to what this means:

jo******@gmail.com wrote:
document.write('<script language="javascript"
src="test1.js"><\/script>');


There are two possible interpretations, and which one a browser uses
probably depends on what some programmer arbitrarily decided to
implement. I doubt that there is rfc that specifies the way this
should work (though I could be wrong).

Interpretation 1 is kind of like depth-first tree traversal, and this
is what you expected to happen. The browser executes the first
document.write, and then evaluates what's written. In this case, what
you've written was more javascript, so the browser then executes that.
That javascript writes out LINE 1. Then the browser pops up out of the
tree and continues what it was doing, and writes out LINE 2.

Interpretation 2 is kind of like a breadth-first traversal. The
browser executes the first document.write, but doesn't pay any
attention to what it wrote out. It continues on to the second write
and outputs LINE 2. Then after it's done at that level (doing the
breadth of the tree), it probably passes the result to the renderer and
the renderer notices that, "hey there's more javascript here!" and
proceeds to process the second level of the tree, writing out LINE 1.

If my analysis is correct, then you can't do anything about this.

I wonder why you're using javascript to write javascript, but I'm sure
you had a good reason. If you could possibly stop doing that, I think
you'll get around this problem.

Feb 21 '06 #4
> I wonder why you're using javascript to write javascript

Well, it's definitely one way to achieve modularity, but apparently not
a very effective way. :-)

I think that Plan B is going to involve using PHP as the language for
my external JS files. That way, I can "include" whatever additional
javascript I want without worrying about the vagaries of the browser's
javascript interpreter.

Thanks for the feedback,
John

Feb 21 '06 #5
jo******@gmail.com wrote:
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?


Script elements can't be nested, so the browser has to move the nested
element to after the 'enclosing' element.

I suppose error correction goes something like: having encountered an
opening tag for a script element, the browser decides to write the
second element after the first element's closing tag. So the nested
script will be moved to after the first script always.

I would expect that to be pretty consistent in all browsers.
--
Rob
Feb 22 '06 #6
jo******@gmail.com wrote:

Please provide attribution of quoted material.[1]
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
I wonder why you're using javascript to write javascript
Well, it's definitely one way to achieve modularity, but apparently not
a very effective way. :-)


Indeed.
I think that Plan B is going to involve using PHP as the language for
my external JS files. That way, I can "include" whatever additional
javascript I want without worrying about the vagaries of the browser's
javascript interpreter.


Since you "include" the script without condition, there is no reason why
you could not write it as it is. You do not need server-side PHP for that.

| <script language="javascript">

(Did someone already mention that the `language' attribute is deprecated,
and the type attribute is required for Valid HTML 4/XHTML 1?)

| Â* document.write('<script language="javascript"
| src="test1.js"><\/script>');
| document.write('<p>LINE 2: Should appear below LINE 1, but does
| not...</p>');
| </script>

<script type="text/javascript" src="test1.js"></script>
<script type="text/javascript">
document.write('<p>LINE 2: Does appear below LINE 1<\/p>');
</script>

As simple as that :)

Note that you have to escape ETAGO delimiters (`</') _always_ within
the _HTML_ `script' element, or other elements with CDATA-type content,
not only for `</script>'.
HTH

PointedEars
___________
[1] <URL:http://safalra.com/special/googlegroupsreply/>
Feb 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: SpaceCowboy | last post by:
To begin, I'm using JBuilder9 under J2ME and MIDP 1.0. I'm trying to use an external library. I can get my code to compile, but I can't get the library code working on the emulator. I'm...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
20
by: Nick | last post by:
Right now I'm using document.write("<script language='javascript' src='jsFile" + i + ".js'></script>"); It works -- I have a lot of data in each file and only want the visitor to have to...
8
by: David D. | last post by:
In html, one can say <script language="JavaScript" src="http://someCodeSnippet.js"> Is there any way to embed the included code snippet in a function (in the case where it is not already a...
10
by: Pasquale | last post by:
hello wverybody... i've got a terrible matter with JS my browsers (either IE and NN) load the external scripts uncorrectly...they load the files from the half part of them and not from the...
5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.