Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Script-Include and inline JS race condition?

Reply
Thread Tools

Script-Include and inline JS race condition?

 
 
Juergen Stein
Guest
Posts: n/a
 
      11-10-2005
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>
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-10-2005


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/
 
Reply With Quote
 
 
 
 
web.dev
Guest
Posts: n/a
 
      11-10-2005

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.

 
Reply With Quote
 
Juergen Stein
Guest
Posts: n/a
 
      11-10-2005
Thanks for the clarification.

Awesome smart these browsers

-j
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple functions (one version being inline and other beingnon-inline) Rahul C++ 3 02-28-2008 03:28 PM
about extern inline and static inline Sean C++ 4 04-30-2006 03:18 PM
To inline or not to inline? Alvin C++ 7 05-06-2005 03:04 PM
Mega Pixel race is like the Mhz Race Hugo Drax Digital Photography 7 01-12-2004 11:07 AM
inline or not to inline in C++ Abhi C++ 2 07-03-2003 12:07 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57