Include javascript files

R

Robert

Hi,
I have some questions regarding including javascript files.

<head>
<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>
</head>

Is it safe to assume that file1.js is always completely before any
statement is executed in file2.js?

One other question is if it is possible to include a javascript file,
but not getting an error about it if this file does not exist (in IE6,
Firefox and other modern browsers)?

Robert.
 
C

cwdjrxyz

Robert said:
Hi,
I have some questions regarding including javascript files.

<head>
<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>
</head>

Is it safe to assume that file1.js is always completely before any
statement is executed in file2.js?

Probably not always. Suppose file one contains a time delay of several
seconds and then uses document.write to write some data or code that
will be called for at once when you reach file2.
One other question is if it is possible to include a javascript file,
but not getting an error about it if this file does not exist (in IE6,
Firefox and other modern browsers)?

See http://www.cwdjr.net/test/jsgroup.html . This has a reference to an
external script at http://www.hell.net/fireandsmoke.js. I get no error
message on Firefox or at the W3C validator. I have not had time to try
other browsers yet.
 
R

Randy Webb

Robert said the following on 3/22/2006 10:29 AM:
Hi,
I have some questions regarding including javascript files.

<head>
<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>
</head>

Is it safe to assume that file1.js is always completely before any
statement is executed in file2.js?

One other question is if it is possible to include a javascript file,
but not getting an error about it if this file does not exist (in IE6,
Firefox and other modern browsers)?

If you try to load an external file in IE6 that doesn't exist then about
95% of the time you are going to get an "Invalid Character" syntax error.
 
R

Robert

Randy said:
Robert said the following on 3/22/2006 10:29 AM:

If you try to load an external file in IE6 that doesn't exist then about
95% of the time you are going to get an "Invalid Character" syntax error.

Yes I know, that's why I ask :)
 
R

Randy Webb

Robert said the following on 3/22/2006 4:46 PM:
Yes I know, that's why I ask :)

It gives the same behavior if you try to dynamically create the script
element as well:

oScript = document.createElement('script');
oScript.src='fakeJSFile.js';
document.getElementsByTagName('head')[0].appendChild(oScript);

Invalid Character error.

You could try retrieving the file with an XMLHTTPRequest object but that
seems like trying to use a sledgehammer to install trim in your house -
overkill.

Typically, you will end up better off not trying to cope with it and let
the error message be an indication of filename/path problems to the
author. If you are retrieving the files from another server, retrieve
them using your own server and then serve it to the browser. If the file
doesn't exist on the remote server, then your server can let the browser
know that and the user never gets an error from a non-existent file.
 

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
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top