Allan Jensen

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromAllan Jensen
ToMe
SubjectMistake in your Javascript tutorial?!
Date7 April 2006 08:13
Hi

In your excellent Javascript tutorial I'm wondering about the example you
give on obtaining _mouse_ coordinates. You write:

if( document.captureEvents && Event.KEYUP ) {
	//remove this part if you do not need Netscape 4 to work
	document.captureEvents( Event.KEYUP );
}
myReference.onmousemove = alertCoord;
...

Shouldn't Event.KEYUP be Event.MOUSEMOVE instead??

Regards
FromMe
ToAllan Jensen
SubjectRe: Mistake in your Javascript tutorial?!
Date7 April 2006 08:23
Allan,

>     document.captureEvents( Event.KEYUP );
>
> Shouldn't Event.KEYUP be Event.MOUSEMOVE instead??

Indeed it should. Thanks for pointing that out. I have now updated the page.

Cheers.

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromAllan Jensen
ToMe
SubjectStopping all Javascript execution
Date7 May 2006 18:33
Hi Mark,

Wonder if you in 10 seconds can tell me whether or not it is possible to on
demand stop ALL FURTHER Javascript processing in Opera?

I have this error handling function which attempts to do it - it works fine
in 
IE but Opera still comes up with an error:

function js_error(msg) {
	msg = "Sorry, there was a problem...\n\n- "+ msg +"\n\nPlease contact the 
website developer.";
	alert(msg);
	window.onerror = myErrorHandler;
	function myErrorHandler() {
		return true;  //it seems this needs to return true
	}
	throw "Further execution has been cancelled on purpose.";
}

I make my own error handler which does nothing and then throw an error to
stop any further processing - silently. But Opera still comes up with an
error:

Unknown thread
Unhandled exception: "Further execution has been cancelled on purpose."

Can I somehow get rid of that message?

Thanks for your time.

Regards
FromMe
ToAllan Jensen
SubjectRe: Stopping all Javascript execution
Date7 May 2006 23:27
Allan,

> I have this error handling function which attempts to do it - it works
> fine in IE but Opera still comes up with an error:

Opera does not support the window.onerror event, so it continues to run the
script. If you want to trap errors in all current browsers (including
Opera), wrap your code in a try...catch block instead. You can even wrap
this around your entire code. Alternatively, just wrap it around the
particular piece of code that might produce an error, or around the function
calls that may have errors somewhere within them.

If the case of your example, this would be:

function js_error(msg) {
    msg = "Sorry, there was a problem... etc";
    alert(msg);
    try {
    throw "Further execution has been cancelled on purpose.";
    } catch(er) {}
}


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromAllan Jensen
ToMe
SubjectRe: Stopping all Javascript execution
Date8 May 2006 10:28
Mark,

> 	try {
> 	throw "Further execution has been cancelled on purpose.";
> 	} catch(er) {}

Well, the thing is that NO CODE should be run after this function has been 
called - and the above does not prevent that. Using "return" doesn't help 
either because it only terminates the rest of the code in the function. Making 
a try...catch everytime I think something might go wrong will make quite a 
complicated and confusing script - it will be a lot of "levels" in the end. 
That's why I used the onerror event as that was the only way I could think of 
to stop the browser from processing javascript. It's the equivalent to the PHP 
exit() command that I'm looking for. But maybe I simply have to live without 
it... :(

One second thing, I'm a little unsure about how often I should check for the 
browser supporting a specific method or property. I'm using a generic 
referencing function but besides that should I check for eg. a table object 
containing a rows collection, check for specific style attributes etc. Do you 
know of any good overview of that from which I could learn, or should I just 
check everything?!

Regards
FromMe
ToAllan Jensen
SubjectRe: Stopping all Javascript execution
Date8 May 2006 13:20
Allan,

> Well, the thing is that NO CODE should be run after this function has been
> called - and the above does not prevent that.

put the try...catch around _everything_:

try {
 function foo() {
  etc
 }
 function bar() {
  etc
 }
 function handleErrors() {
  var msg='something went wrong. please contact me';
  throw({name:'my error',message:'an error'});
 }
 ...etc...
} catch(er) {}

That is as close as you will get to the desired behaviour without throwing
an error that gets caught by the console (although since the console does
not normally appear, this would also work).

New events triggered after this will still run, however.

> One second thing, I'm a little unsure about how often I should check for
> the browser supporting a specific method or property.

Every time there is a browser that does not support them; that's the simple
idea, at least. There is always something picky somewhere, but generally, I
find PPK's tables to be very useful here:
http://www.quirksmode.org/dom/w3c_core.html
http://www.quirksmode.org/dom/w3c_html.html
http://www.quirksmode.org/dom/w3c_css.html
http://www.quirksmode.org/dom/w3c_events.html

Look for the methods or properties you intend to use. If one of those
browsers fails to support it, check for it before using it (unless you are
intentionally ignoring that browser - Netscape 4, for example can be ignored
for all DOM scripting, since it does not support DOM).


Tarquin
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.