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 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."
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."
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |