Script-Include and inline JS race condition?

J

Juergen Stein

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>
 
M

Martin Honnen

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.
 
W

web.dev

Juergen said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top