473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help: Program flow when error in onclick event?

I was wondering,

what happens when you have an onclick event and an error occurs in it:

In an <a> element:
onclick="zoomFullExtent(); return false;"

I know that there is an error happening in zoomFullExtent. I didn't
define my own error handler, so the default one is used.(My browser is
Firefox 1.0).

I notice that when this error happens, the browser makes a request to
the server.
I thought that if an error happened in zoomFullExtent, the default
error handler would catch it, and then zoomFullExtent would return
normally. But that doesn't seem to happen. Instead the whole onclick
script returns or is aborted? And it seems to return true so that the
request is made. Is there a page where this program flow is explained?

Thanks for reading,

Roland
Jul 23 '05 #1
4 1931
Lee
Roland said:

I was wondering,

what happens when you have an onclick event and an error occurs in it:

In an <a> element:
onclick="zoomFullExtent(); return false;"

I know that there is an error happening in zoomFullExtent. I didn't
define my own error handler, so the default one is used.(My browser is
Firefox 1.0).

I notice that when this error happens, the browser makes a request to
the server.
I thought that if an error happened in zoomFullExtent, the default
error handler would catch it, and then zoomFullExtent would return
normally. But that doesn't seem to happen. Instead the whole onclick
script returns or is aborted? And it seems to return true so that the
request is made. Is there a page where this program flow is explained?


It doesn't have to return true. The link is followed unless the onclick handler
specifically returns false. The best solution is probably to avoid using a link
in that case.

Jul 23 '05 #2
Roland wrote:
I was wondering,

what happens when you have an onclick event and an error occurs in it:

In an <a> element:
onclick="zoomFullExtent(); return false;"

I know that there is an error happening in zoomFullExtent. I didn't
define my own error handler, so the default one is used.(My browser is
Firefox 1.0).

I notice that when this error happens, the browser makes a request to
the server.
I thought that if an error happened in zoomFullExtent, the default
error handler would catch it, and then zoomFullExtent would return
normally. But that doesn't seem to happen. Instead the whole onclick
script returns or is aborted? And it seems to return true so that the
request is made. Is there a page where this program flow is explained?

Thanks for reading,

Roland


Uncaught errors may exit one or more execution contexts - so it is
possible for an error in 'zoomFullExtent()' to cause your onclick to
exit. If that happens, your onclick will return something other than
'false' and the link will be followed.

Where an onclick is used to intercept the default action of an HTML
element (say an <a href="... ), it is normal for the function to
return an appropriate value that controls whether or not the action
is performed, e.g.

<a href="..." onclick="return zoomFullExtent();" ...>

and in zoomFullExtent():

function zoomFullExtent(){
var OK = false;

// do some stuff, test that everything works as expected

if ( /* everything OK */ ) {
OK = true;
}
return !OK;
}

This may not suit you in this case though. Look in the ECMA spec
section 10.2.

--
Rob
Jul 23 '05 #3
"...it is normal for the function to return an appropriate value that
controls whether or not the action is performed"

No, actually, with click handlers on buttons and links, returning true
or null will allow the link to be processed as if your click handler
didn't exist. You will almost always want to return false. What you
probably want is something like this:

<a href="#" onclick="return zoomFullExtent();">link</a>

and in zoomFullExtent():
var debug = true;
function zoomFullExtent() {
try {
//do your normal stuff
} catch( e ) {
if( debug ) alert( "Error in zoomFullExtent: " + e );
}

return false;
}

Note that the try...catch blocks handle any errors you receive. Setting
debug to false will hide the errors from end users. And, most
importantly, the function always returns false.

Jul 23 '05 #4
Gregory wrote:
"...it is normal for the function to return an appropriate value that
controls whether or not the action is performed"

No, actually, with click handlers on buttons and links, returning true
or null will allow the link to be processed as if your click handler
didn't exist.
Yes, that's exactly what is required in most cases (of course we
speak in terms of our own experience here)
You will almost always want to return false. What you
probably want is something like this:

<a href="#" onclick="return zoomFullExtent();">link</a>
No, you want the onclick handler to do something and if the
client-side JavaScript does not do it, then the link should head off
to the server where something appropriate happens - either the
functionality is provided server-side or an explanation is given as
to why it didn't happen.

Therefore only if the script does what it's supposed to should
'false' be returned. Any other value causes the link to be followed -
this logic is inherent in the architecture of intrinsic events.

and in zoomFullExtent():
var debug = true;
function zoomFullExtent() {
try {
//do your normal stuff
} catch( e ) {
if( debug ) alert( "Error in zoomFullExtent: " + e );
}

return false;
}

Note that the try...catch blocks handle any errors you receive. Setting
debug to false will hide the errors from end users. And, most
importantly, the function always returns false.


So if the script errors, nothing useful happens? It also suggests
that all functions called by intrinsic events should have try/catch
blocks. Some of the more learned contributors may wish to comment on
that proposition.
--
Rob
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: hope | last post by:
Hi Access 97 I'm lost on this code please can you help ================================= Below is some simple code that will concatenate a single field's value from multiple records into a...
1
by: MDBloemker | last post by:
can anyone help me fathom out how to use this bit of code: Public Class Utilities Public Shared Sub CreateConfirmBox(ByRef txt As WebControls.TextBox, _ ByVal strMessage As String)...
7
by: Tigger | last post by:
Dear Experts, I am working on ASP.NET. I have got a problem related to the usage of Javascript in ASP.NET. Please help. The story is the following: 1) I am developing an ASP.NET application. I...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
6
by: drec | last post by:
I am just learning Javascript and I would like to create a basic form that gives me two options. This will be using either checkbox or radio input type, however I would like the second option to...
19
by: pamelafluente | last post by:
Hi Guys, I am trying to include my little script in my html report. I have done an external JS file which contains it. If you remember, you have helped me to detect if the asp page was present...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
17
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.