473,396 Members | 2,036 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.

Race Condition with Javascript - Any ideas?

Hello there.

I have a script (say, page.js) called from a script tag as such:
<script src="page.js"></script>

Within page.js, I have something like the following:

document.write("<html><head><script type=\"text/javascript\"
src=\"/myobject.js\"><\/script></head>");
document.write("<body><script type=\"text/javascript\">var o=new
MyObject();<\/script></body></html>");

This works fine in Firefox, but in IE, 50% of the time, I get a message
saying "Error: MyObject is undefined". If I add a "defer" attribute to
the script tag, I get the error less frequently. I am pretty sure this
is caused by a race condition. I would like to think that the contents
of myobject.js will be loaded first, preserving sequence, but this does
not appear to be the case... To clarify, myobject.js contains the
definition for the MyObject object.

Any suggestions or insights are appreciated.
Thanks,
Chris

Jul 12 '06 #1
3 2986
chris johnson wrote:
Hello there.

I have a script (say, page.js) called from a script tag as such:
<script src="page.js"></script>

Within page.js, I have something like the following:

document.write("<html><head><script type=\"text/javascript\"
src=\"/myobject.js\"><\/script></head>");
document.write("<body><script type=\"text/javascript\">var o=new
MyObject();<\/script></body></html>");
I won't ask why...
This works fine in Firefox, but in IE, 50% of the time, I get a message
saying "Error: MyObject is undefined". If I add a "defer" attribute to
the script tag, I get the error less frequently. I am pretty sure this
is caused by a race condition. I would like to think that the contents
of myobject.js will be loaded first, preserving sequence, but this does
not appear to be the case... To clarify, myobject.js contains the
definition for the MyObject object.

Any suggestions or insights are appreciated.
var o=new MyObject();
is executed as the page is parsed.

Add something like this to the head <script>:
function init() {
var o=new MyObject();
}

Then do:
<body onload="init()">

I'll bet that will work. It will wait until the page is loaded & parsed
before trying to execute it.

--
"The most convoluted explanation that fits all of the made-up facts is
the most likely to be believed by conspiracy theorists. Fitting the
actual facts is optional."
Jul 12 '06 #2
Thanks for the reply Tony.

I didn't think about going that route, but after some experimentation,
the <body onload="..."technique seemed to work great. I appreciate
the guidance.

Logically however, it still surprises me that the script is not loaded
sequentially.

Again, many thanks.
-Chris

Tony wrote:
chris johnson wrote:
Hello there.

I have a script (say, page.js) called from a script tag as such:
<script src="page.js"></script>

Within page.js, I have something like the following:

document.write("<html><head><script type=\"text/javascript\"
src=\"/myobject.js\"><\/script></head>");
document.write("<body><script type=\"text/javascript\">var o=new
MyObject();<\/script></body></html>");

I won't ask why...
This works fine in Firefox, but in IE, 50% of the time, I get a message
saying "Error: MyObject is undefined". If I add a "defer" attribute to
the script tag, I get the error less frequently. I am pretty sure this
is caused by a race condition. I would like to think that the contents
of myobject.js will be loaded first, preserving sequence, but this does
not appear to be the case... To clarify, myobject.js contains the
definition for the MyObject object.

Any suggestions or insights are appreciated.

var o=new MyObject();
is executed as the page is parsed.

Add something like this to the head <script>:
function init() {
var o=new MyObject();
}

Then do:
<body onload="init()">

I'll bet that will work. It will wait until the page is loaded & parsed
before trying to execute it.

--
"The most convoluted explanation that fits all of the made-up facts is
the most likely to be believed by conspiracy theorists. Fitting the
actual facts is optional."
Jul 13 '06 #3

chris johnson wrote:
Thanks for the reply Tony.
Please don't top post in comp.lang.javascript.

I didn't think about going that route, but after some experimentation,
the <body onload="..."technique seemed to work great. I appreciate
the guidance.
Readers are left to wonder: what route?
Logically however, it still surprises me that the script is not loaded
sequentially.
Resource downloads start sequentially but may finish in a different
order.

[...]
This works fine in Firefox, but in IE, 50% of the time, I get a message
saying "Error: MyObject is undefined". If I add a "defer" attribute to
the script tag, I get the error less frequently.
The defer attribute is just a suggestion that the script can be
executed later as it doesn't affect the display of elements in the
page. I don't think it has any effect on the downloading or parsing of
the script itself.

[...]
Any suggestions or insights are appreciated.
var o=new MyObject();
is executed as the page is parsed.
If you have a large page, waiting for onload might be too late. You
could try using setTimeout, something like:

function init(){
if (MyObject){
// use MyObject
} else {
// wait a little... say 0.1 seconds
setTimeout('init();', 100);
}
}

init();

--
Rob

Jul 13 '06 #4

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

Similar topics

5
by: GIMME | last post by:
One of my coworkers insists that one should never use static methods because race conditions exist. Thinking along the lines that all variable assignments are assignments to static variables. ...
3
by: Juergen Stein | last post by:
Hi Group, I couldn't find an answer on this with Google, so let me test you :) I've a fairly complex WebApp, and I put most of the JS code in independent external .js files. One of these...
2
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in...
0
by: Richard | last post by:
Hi, I'm suffering a socket race condition - I think. The code works fine at full speed on a single CPU machine. However when I run full speed on a 2 Zeon machine the socket drops data on an...
5
by: anonymous | last post by:
I'm writing a program that deals extensively with the printer. For the most part my application runs fine, but occasionally I run into some Exceptions. The most common exceptions I run into are...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
3
by: Ryan Liu | last post by:
Hi, What does ArrayList.Synchronized really do for an ArrayList? Is that equal to add lock(this) for all its public methods and properties? Not just for Add()/Insert()/Remvoe()/Count, but also...
2
by: antonyliu2002 | last post by:
I do not quite understand the race condition. As I posted a couple of days ago, I create a PDF on the fly in my web application at regular intervals. Users will be able to download the PDF...
7
by: Berryl Hesh | last post by:
I wouldn't nomally post this here, as it has something to do with the ListView control usage I think, or maybe with a race condition or some windoes messaging. I'm just not sure. The test below...
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.