how to capture onerror without ending script blocks

T

Tobius

I want to be able to define a custom onerror event that detects an
attempt to call a known function and load a file if it's not already
loaded and re-call the function. The only problem is that any
triggering of the onerror event stops execution of the script block in
question. Is there a workaround for this?

Here's an example of what I'm trying to accomplish:

==================================================

<!-- begin test.html -->

<html>
<head>
<title></title>
<script type="text/javascript">

function customerror(msg, url, line)
{
if (msg == 'customfunction is not defined')
{
var js = document.createElement("script");
js.src = 'customfunction.js';
js.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(js);
}
return(true);
}

window.onerror = customerror;

alert('before error');
customfunction();
alert('after error'); // the error above prevents this line from
executing

</script>
</head>
<body>

some html text
<script type="text/javascript">
alert('inside body');
</script>

</body>
</html>

<!-- end test.html -->

==================================================

/* begin customfunction.js */

alert('customfunction.js loaded');

function customfunction()
{
alert('inside customfunction');
}

/* end customfunction.js */

==================================================

Aside from the missing feature detection, forwarded function
parameters, etc (this is just a prototype), the problem is that any
code executed after the invalid function call is made gets skipped. It
seems that any error kills the rest of the script block ... is there a
way around this without putting every line of code on its own script
block?

Oh yeah, putting each command in its own script block seems to work
around this "problem", but that seems like a terrible solution. I know
I could just create a loading wrapper for all of the functions in
question, but I wanted something more transparent than that.

-tm
 
T

Thomas 'PointedEars' Lahn

Tobius said:
I want to be able to define a custom onerror event that detects an
attempt to call a known function and load a file if it's not already
loaded and re-call the function. The only problem is that any
triggering of the onerror event stops execution of the script block in
question. Is there a workaround for this?

I am afraid no. Besides, `onerror' is proprietary.
Here's an example of what I'm trying to accomplish:
[...]
function customerror(msg, url, line)
{
if (msg == 'customfunction is not defined')
{
var js = document.createElement("script");
js.src = 'customfunction.js';
js.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(js);
}

Here you would want to assign `null' to `window.onerror'.
return(true);
}

window.onerror = customerror;

alert('before error');
customfunction();
alert('after error'); // the error above prevents this line from
executing

You are doing this the wrong way. Instead of waiting that the error
occurs, you should do a feature test so that no error can occur.

if (typeof ... != "function")
{
loadScript(...);
}

if (typeof ... == "function")
{
... ...(...) ...
}

That said, loading script code this way is recommended against, because
it is known to be unreliable and not backed up by any standard.


HTH

PointedEars
 
T

Tobius

Are there any modern browsers that don't support onerror? My targeted
browser support list is:

Internet Explorer 5.5+
Mozilla Firebird 1.05+
Safari 1.2+

Have you witnessed this event fail to work in any of those browsers?

How does setting "window.onerror = null" help me with this exercise?
Does this allow me to detect an undefined function call in some way of
which I'm failing to see?

Anyhow, the whole point of this exercise is to be able to dynamically
load a library function "as" it's needed instead of "in case" it's
needed. The purpose is to allow a javascript developer to have all of
their javascript libraries available to them on every site they develop
without requiring having to over-bloat their web pages or manually
include sub-sets of their libraries on individual pages. Have you ever
seen anyone doing anything like this?
 
T

Thomas 'PointedEars' Lahn

Tobius said:
Are there any modern browsers that don't support onerror?
Possible.

My targeted browser support list is:

Internet Explorer 5.5+
Mozilla Firebird 1.05+

Maybe you have not heard yet: The browser is called Firefox since its
version 0.8 (Royal Oak, released Febrary 9 2004 CE). There has never
been "Mozilla Firebird 1.05".
Safari 1.2+

Have you witnessed this event fail to work in any of those browsers?

Depends on what you mean by that. That following code is not executed
even though `true' is returned in Firefox 1.5.0.1/Linux could be considered
a failure, because in Mozilla/4.0 (NN 4.x), where this error-handling
approach originates from, it did not happen the last time I checked.
But then, IIRC I never tried to work around ReferenceErrors with it.
ReferenceErrors are known to be somewhat special regarding error/exception
handling. Search the archives.
How does setting "window.onerror = null" help me with this exercise?

It prevents that the error-handling function is called again and again,
even from within the error-handling function. Maybe that helps. So
probably this should be even the first statement within that function.
Does this allow me to detect an undefined function call in some way of
which I'm failing to see?

Perhaps. However, with your targeted environments, if you insist risking
the error and handling it later (what I recommended against and provided
a viable alternative for, which apparently you have ignored kindly), you
should use try..catch instead. I use the `onerror' event handler iff I
want to provide for graceful degradation for not backwards-compatible
language features (particularly for NN4), such as try..catch.
Anyhow, the whole point of this exercise is to be able to dynamically
load a library function "as" it's needed instead of "in case" it's
needed. The purpose is to allow a javascript developer to have all of
their javascript libraries available to them on every site they develop
without requiring having to over-bloat their web pages or manually
include sub-sets of their libraries on individual pages. Have you ever
seen anyone doing anything like this?

I have, and I have commented on it already, here.


PointedEars
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top