473,403 Members | 2,284 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,403 software developers and data experts.

Script-Include and inline JS race condition?

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 external files contains a
class A. The inline JS then inherits a class B from A.

My question is now, since the class A is defined in the external file,
is the class A definitely known at the point where I use it? I could
imagine a case where the external js is loaded much slower, so the
js-parser would already try to new A() without knowing of it.

If I would run in a race condition, how could I get around it?

Thanks in advance.
-j

The html looks like this:

<html>
<head>
<script src="class_a.js" type="text/javascript"></script>
</head>
<body>
...
</body>
<script type="text/javascript">
// inline code
function B()
{
this.is="cool";
}

B.prototype = new A();
B.prototype.constructor=B;
</script>
</body>
</html>
Nov 10 '05 #1
3 2403


Juergen Stein wrote:

I've a fairly complex WebApp, and I put most of the JS code in
independent external .js files. One of these external files contains a
class A. The inline JS then inherits a class B from A.

My question is now, since the class A is defined in the external file,
is the class A definitely known at the point where I use it? I could
imagine a case where the external js is loaded much slower, so the
js-parser would already try to new A() without knowing of it.


No, the browser, its HTML parser and its calls to the script engine are
smart enough to block HTML parsing until that external script file has
been loaded and the script has been executed. After all the script could
add content with document.write.
If you have
<script src="file.js" type="text/javascript"></script>
then that will be processed first and you can be assured that further
script has access to the variables and functions defined in file.js as
long as not load problems and no script errors occur.
The exeception would be
<script src="file.js" type="text/javascript" defer></script>
but only IE supports defer anyway as far as I know.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 10 '05 #2

Juergen Stein wrote:
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 external files contains a
class A. The inline JS then inherits a class B from A.

My question is now, since the class A is defined in the external file,
is the class A definitely known at the point where I use it? I could
imagine a case where the external js is loaded much slower, so the
js-parser would already try to new A() without knowing of it.

If I would run in a race condition, how could I get around it?

Thanks in advance.
-j

The html looks like this:

<html>
<head>
<script src="class_a.js" type="text/javascript"></script>
</head>
<body>
...
</body>
<script type="text/javascript">
// inline code
function B()
{
this.is="cool";
}

B.prototype = new A();
B.prototype.constructor=B;
</script>
</body>
</html>


;) The best way to know is to try it out yourself. But yes, in the
example you shown above, class A will always be known to B. When you
include external js files in the head, as the browser loads the page,
it will load each external js file before continuing on to the rest of
the page. There will be a situation where class A won't be available
immediately. Using the same example from above, if you instead include
the external js file after your script for class B, then it wouldn't
work. Because class A is not available yet.

Hope this helps. :)

Nov 10 '05 #3
Thanks for the clarification.

Awesome smart these browsers :)

-j
Nov 10 '05 #4

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

Similar topics

6
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java...
1
by: bayouprophet | last post by:
Cant get menu script to to put linked page in the same frame. I am new to Java and I am wondering what am I doing wrong? below are my java applet file, frame.html file, and my text file and one...
1
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
3
by: P Wolpert | last post by:
This is my first post. I hope I don't sound stupid. I have a script conflict when I put two scripts on one page. Both scripts will work if I use one at a time but the menu button script will not...
8
by: Jakej | last post by:
I've been using a javascript in an html file for a banner slider, and it works as desired. But I'd like to use it on more than one page and it would be great if I could transfer the code to a .js...
23
by: Loony | last post by:
I have got a code like this in HTML section in ASP file which includes javascript file! The script works under MS IE but doesn't with Firefox! Can anybody tell me what is wrong? <HTML>...
4
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
2
by: bilaribilari | last post by:
Hi all, I am using Tidy (C) for parsing html pages. I encountered a page that has some script as follows: <script> .... var abc = "<script>some stuff here</" + "script>"; .... </script>
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.