By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,627 Members | 1,870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,627 IT Pros & Developers. It's quick & easy.

Race Condition with Javascript - Any ideas?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.