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 4 1961
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.
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
"...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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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
|
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:
-...
|
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...
|
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...
| |
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.
|
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
|
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?
|
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?
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |