473,657 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.con structor=B;
</script>
</body>
</html>
Nov 10 '05 #1
3 2435


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.con structor=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
12972
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 scripts. Being a javascript newbie and after significant testing, I suspect that the document.write fails after finding a </script> within pageVar. Does a trick exist that enables one to slightly alter pageVar whereby enabling...
1
3246
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 of my link file that should load next to the menu on the same page. And Thank You in advance. Here is my menu applet: <html>
1
4809
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 command in some type of array. I added the mailbox in the lower left corner (see link below)http://www.aamechanical.com/indextemp.htm but I cannot get it to swap. Here is the code for the page: Thanks for your help in advance
11
3055
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 code is function RemoveHTMLScript(strText) { var regEx = /<script\w*<\/script>/g
3
1768
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 work when I add the scrolling text script. One script, the menu button script, has an "Onload" command and the other one does not. From what I know, and that's not much, I need to put both scripts in the "Onload" command. I just don't know how to...
8
4665
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 file and call it with the <script src="filename.js"></script> tags as I do for many other .js files. But, when I try to do that way, it doesn't work right. It will display the banner text, but only on the 0,0 page coordinate and doesn't slide....
23
16129
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> <HEAD><TITLE></TITLE> <SCRIPT LANGUAGE="JavaScript" SRC="../inc/JSfile.js"><SCRIPT> <SCRIPT> <!-- other javascript scripts working propely
4
2152
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 broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one line
3
2957
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 of the script gets executed. Can someone please give me a direction as to what I may be missing? Thanks.
2
2964
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>
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8613
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.