SRC= and missing .JS file

  • Thread starter Paul Gorodyansky
  • Start date
P

Paul Gorodyansky

Hi,

Could not find the answer neither in FAQs not on the Web...

On many javaScript 'tips' sites they say that do warn the user
regarding missing .JS file one should do the following:
<html>
<head>
....
<script src="ABC.js" ...>
alert("Error! Included JS file not found ...");
</script>
</head>
....

But it does NOT work neither in my IE 6 nor in my Mozilla 1.6 -
no alert when the .JS file does not exist.

http://www.htmlhelp.com/reference/html40/special/script.html

says,
"When the SRC attribute is used, the embedded script is ignored."
which does not touch the case when .JS not found
(or does it? Ignored _always_?)

So how to tell the user that .JS to be included was not found?

--
Regards,
Paul
http://KBD.da.ru - was possible to develop only because
of the great article at
http://www.faqts.com/knowledge_base/view.phtml/aid/1661
 
P

Paul Gorodyansky

Hi,
I've been experimenting with the following and it seems to work. In the
.js file put the following statement at the top.
var script111loaded = "true";

Now in the HTML put
<script type="text/javascript">
if (!script111loaded) {alert("script 111 not loaded")};
</script>

Thanks, it works (though, I had to declare that variable
at the top of my _html_ text,too - otherwise _if_ did not
work - just has "undefined" error in JS Console, at least
in Mozilla 1.6).
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
Could not find the answer neither in FAQs not on the Web...

So how to tell the user that .JS to be included was not found?

I test for the existence of a variable declared in it.

I don't know about putting material here --------+
V
<script type="text/javascript" src="include1.js"> </script>
 
D

Dr John Stockton

JRS: In article <090420041736545389%[email protected]>, seen in
news:comp.lang.javascript said:
I've been experimenting with the following and it seems to work. In the
.js file put the following statement at the top.
var script111loaded = "true";

Now in the HTML put
<script type="text/javascript">
if (!script111loaded) {alert("script 111 not loaded")};
</script>


Why did you choose to assign a string to script111loaded ?
<rhetorical> How about "false", "banana", or just 1 ?
 
Y

Yann-Erwan Perio

if(!this.script111loaded) //...
Why did you choose to assign a string to script111loaded ?
<rhetorical> How about "false", "banana", or just 1 ?

Well, any non-empty string would technically type-convert to true,
though I question the semantic value of "banana" for maintenance
purposes... :)

As you suggest, using a boolean instead of a string would definitely be
a good idea!
 
R

Richard Cornford

I can understand why a script may care that a file on which it is
dependent has not loaded, but I don't see much point in telling the user
in most cases as there is not going to be a great deal that they can do
about it (assuming that they understand the role of a JS file in the
first place).
I test for the existence of a variable declared in it.

I don't know about putting material here --------+
V
<script type="text/javascript" src="include1.js"> </script>

One of the problems with this is how the server reacts to its inability
to find the file. A suitable HTTP error response should tell the browser
that something is up, but many servers are configured to send custom
HTML pages that announce the error to the user but might still give the
browser the impression that the resource that was requested has been
returned. And an HTML file sent in the pace of a JS file will produce a
syntax error, but not necessarily result in the fall-back to using the
contents of the script element.

Richard.
 
P

Paul Gorodyansky

Hello!

Richard said:
One of the problems with this is how the server reacts to its inability
to find the file. A suitable HTTP error response should tell the browser
that something is up, but many servers are configured to send custom
HTML pages that announce the error to the user but might still give the
browser the impression that the resource that was requested has been
returned. And an HTML file sent in the pace of a JS file will produce a
syntax error, but not necessarily result in the fall-back to using the
contents of the script element.

Richard.

it was exactly my reason to post the question here - with
my ISP - CompuServe - *nothing* happens - page loads,... -
no error of any kind, so a user is just
confused why the expected functionality failing - _I_
need to explain the user why :)

BTW, obviously I used a _Boolean_ variable after getting
a response here :)
 
M

Michael Winter

On Sat, 10 Apr 2004 08:10:47 -0700, Dennis M. Marks

[snip]
Why doesn't a test like "if (document.getElementById) {xxx}" give an
undefined error in some browsers, or does it?

In Opera, Mozilla and IE, it does. It probably does in every browser.

If the OP wants to avoid getting "undefined" errors, you could do

if( 'undefined' == typeof scriptXXXloaded ) {
// script XXX is not loaded
}

and declare scriptXXXloaded somewhere in the script

var scriptXXXloaded = true;

This will avoid the need to declare the variable in the containing HTML
file. The value of the variable itself is irrelevant, just so long as it's
not undefined.

Mike
 
R

Richard Cornford

Michael said:
On Sat, 10 Apr 2004 08:10:47 -0700, Dennis M. Marks

[snip]
Why doesn't a test like "if (document.getElementById) {xxx}" give an
undefined error in some browsers, or does it?

In Opera, Mozilla and IE, it does. It probably does in every browser.
<snip>

"if (document.getElementById) {xxx}" - will only produce an undefined
error if - document - is undefined (and a different error if null).
Reading a non-existent property form an object returns an undefined
value, which happily type-converts to boolean false for the expression
of the - if - statement (no errors result).

The OP's problem with - if(!script111loaded) { ... }; - is that the
scope chain resolution of the identifier for an undeclared (and
unassigned) global variable has a null base object on the returned
Reference, and the subsequent use of that Reference with the internal -
GetValue - function is required to throw an exception when the base
object is null.

The typeof operator avoids the problem as it has special handling when a
Reference has a null base object, but a property accessor, where the
identifiers before the property name refer to existing objects, will
never return a Reference with a null base object (so it will never throw
an exception when used with - GetValue).

Richard.
 
M

Michael Winter

Michael said:
On Sat, 10 Apr 2004 08:10:47 -0700, Dennis M. Marks

[snip]
Why doesn't a test like "if (document.getElementById) {xxx}" give an
undefined error in some browsers, or does it?

In Opera, Mozilla and IE, it does. It probably does in every browser.

"if (document.getElementById) {xxx}" - will only produce an undefined
error if - document - is undefined (and a different error if null).
Reading a non-existent property form an object returns an undefined
value, which happily type-converts to boolean false for the expression
of the - if - statement (no errors result).

[snip]

The problem isn't with the conditional expression, it is with xxx itself.
I would guess that as xxx hasn't been previously defined, and isn't part
of an assignment (where the variable would become global), the interpreter
is trying to evaluate something that obviously doesn't exist, hence the
undefined error.

Mike
 
R

Richard Cornford

The problem isn't with the conditional expression, it is with xxx
itself. I would guess that as xxx hasn't been previously defined, and
isn't part of an assignment (where the variable would become global),
the interpreter is trying to evaluate something that obviously
doesn't exist, hence the undefined error.

Ah, you are being unusually pedantic. I took the - xxx - as standing in
for some unspecified block of code to be executed if -
document.getElementById - type-converted to true. I usually use - ... -
in that context, which will also predictably error if actually executed.

Richard.
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top