473,748 Members | 4,030 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="zoomFu llExtent(); 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 1966
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="zoomF ullExtent(); 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="zoomFu llExtent(); 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
2413
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 single string separated by a user defined character. There is no error trapping (by design), USE AT YOUR OWN RISK.
1
2752
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) txt.Attributes.Add("onchange", "return confirm('" & strMessage & "');") End Sub End Class
7
1703
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 need to prompt the users with a modal box (with "Yes" and "Cancel" button on it); 2) When the user clicks "Yes" button, I need to do some further processing. If "Cancel" is clicked, of course, stops doing anything; 3) Now the problems are: -...
11
7763
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 the web server performs a database query and displays the results. All of this works fine. I want the user to be able to press the Enter key while the cursor is still in SearchTextBox and have the SearchButton.Click event fire (thus performing...
6
1910
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 allow the user to type in a value. Also, I would like the 2nd option only editable if the button for that option is selected. All I can seem to find is basic examples of forms, and none of which have this feature. The form would look something...
19
2206
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 by using ajax. If not present is gracefully stay silent. The problem is now that when the JS is not present (and it can be) MSIE gives a lot of errors and one has to kill it.
0
5573
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
5
3693
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 for repeated html im using the external js, i mean the TOP, BOTTOM they are repeated in every page, so i include them as functions in the external js and call them. Why it doesn't work?
17
2678
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 things? Shouldn't they say the same thing?
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9548
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.