Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

JavaScript: The Point of No Return?!

Rated 4.24 (Ratings: 18) (Add your rating)

Log in to add a comment
(12 comments so far)

Want more?

 
Picture of Jeff Howden

Jeff Howden

Member info | Full bio

User since: December 13, 1998

Last login: November 30, 2008

Articles written: 21

Question:

I'm not sure why the code I'm looking at has a return here? I took the return out in a similar onclick event and it still worked fine.

onclick="return openPartner(this)"

Answer:

The function the event handler was calling may not have been written to return a value.

In order to understand why this is important though, I want to talk about the return statement in an event handler and what that accomplishes. There are some event handlers that can be cancelled. Canceling an event effectively renders the default behavior of the tag the event handler is attached to useless. Take the following as an example:

<a href="http://www.yahoo.com/" onClick="return false">Yahoo</a>

When the behavior is run in your web browser you end up with something like this link that does absolutely nothing.

Yahoo

You can click on the link all day long and go nowhere. The statement return false is canceling the default behavior of the <a>, which is to navigate to the value of the href attribute. This is a really simple example, but suffice to say, so long as the return statement is passing a boolean value (true or false), you can affect the behavior of the event handlers they are attached to.

To take this into more worldly, practical example, let's consider a link to a rather large file. Let's say we want to be able to warn the user when they click on that link and ask them if they really want to download that file. We can do this using the confirm() dialogue.

Let's look at an intermediary example to see how that works before moving on to the final solution. This example will call the confirm() method and assign the value of the user's action (whether they click the OK button, returning a value of true from the confirm() method or they click the close or cancel button returning a value of false from the confirm() method) to a variable. We will then use an alert() dialogue to display the value of this variable.

<a href="bigfile.zip" onClick="confirmDownload = confirm('Are you sure?'); alert(confirmDownload)">Download</a>

When the HTML is run in your web browser you end up with something like this link that when clicked displays first a confirm() dialogue and then an alert() dialogue containing the boolean returned by the confirm() dialogue.

Download

Using the return statement within the event handler, we can alter the behavior of the <a> tag depending on what button the user clicks. All we have to do is complete the return statement in the onClick event handler with a boolean which will be stored from confirm() dialogue in the confirmDownload variable.

<a href="bigfile.zip" onClick="confirmDownload=confirm('Are you sure?'); alert(confirmDownload); return confirmDownload">Download</a>

When the above is run, the following is produced:

Download

The return statement is looking for a boolean value (true or false, the same kind of value the confirm() dialogue returns). Knowing this we can use the confirm() dialogue (and consequently the value it will return) as the boolean in the return statement for the event handler.

<a href="bigfile.zip" onClick="return confirm('Are you sure?')">Download</a>

Download

I hope that the use of the return statement makes a little more sense now.

.jeff

Jeff Howden (.jeff) is a web developer working for Vos & Howden, LLC in Portland, Oregon where he's partnered with long-time colleague, Anthony Vos. His skills include ColdFusion, JavaScript, CSS, XML, relational databases, and much, much more. His biggest professional accomplishments include, but are not limited to:

  • building a ColdFusion-based e-commerce solution for Mt. Bachelor that transacted over $1.62 million dollars in September 2001 with 0 (yes, that's zero) ColdFusion errors and then an almost completely rebuilt version transacted $2.86 million dollars in September 2002.
  • being asked to be a Technical Editor for the ColdFusion MX book, Inside ColdFusion MX from New Rider's Publishing company.
  • being asked by BrainBench to perform quality control on their JavaScript 1.5 certification test after receiving the highest beta test score out of 200 testees.
  • managing the server that hosts evolt.org and withstanding a slashdotting that brought over 1,000,000 hits to the site, over 10 gigs of data transfer, and an average in excess of 2300 unique visitor sessions per hour, all within a 24-hour period and the server never hiccuping once.

confirm in Mac IE4, IE5

Submitted by rodneyreid on March 5, 2001 - 22:15.

Jeff, Thanks for the great article --- this explains something that was not too clear to me! I have one gotcha though I learned from testing out some stuff at work: "confirm()" isn't supported on the Mac IE4 and IE5. The San Jose microsoft group (who created IE for the Mac) didn't add it, because it's not 'standard'. Another case where standards fall down!!!! GRRR! WHY WHY didn't the W3c ever make this standard?!?!?!??!?!?! ...Rodney (http://www.toxicorp.com)

login or register to post comments

RE: confirm in Mac IE4, IE5

Submitted by Jeff Howden on March 5, 2001 - 23:11.

I'm glad you liked the article.

I have to disagree with you on support for the confirm() method in Mac IE4/5. I can find no evidence that it's not included in the JavaScript implementation that exists in these browsers. It would be preposterous for Microsoft to not include support for this most basic of dialogues in their web browser as it has been a standard part of the web-browser specific extensions to the core JavaScript language since JavaScript version 1.0.

I tested the theory (on this article of all things) that the confirm() method did not work on a Mac running IE5 (I don't have access to Mac IE4 at this time) and not only did I find that the confirm() method presented me with the usual dialogue, but it also returned the appropriate boolean value indicating my interaction with it. Try this link for example. When clicked it should present you with a boolean indicating support for the confirm() method.

Test support for the confirm() method.

Based on these preliminary findings I'd be curious to know the exact browser and JavaScript you were unable to get working properly.

login or register to post comments

confirm in Mac IE4, IE5

Submitted by rodneyreid on March 5, 2001 - 23:16.

Hi Jeff,

It was IE 4.5 and IE 5.0 on a Mac G3 - one of the QA machines we have at work. I was really mad when I couldn't get it to work, I'm really curious now, I'll try again tomorrow.

Thanks for the quick reply!

...Rodney

login or register to post comments

the download thing

Submitted by HOTRNAIL on June 18, 2001 - 22:35.

the file download thing popped up no matter if yes or no was selected. using ie 5.5. Or is my download manager screwing things up?

login or register to post comments

Re: the download thing

Submitted by Jeff Howden on June 18, 2001 - 22:52.

Hi (Josh is it?),

I've got a couple of questions for you:

  1. What OS did you experience this with?
  2. Is JavaScript enabled in your browser?
  3. Do you get the same behavior whether or not the Download Manager is running?
  4. What happens when you click on the link "Test support for the confirm() method" above?

login or register to post comments

Browser comparisons

Submitted by Dewara on June 19, 2001 - 07:18.

Just a quick question. Where can I find browser comparisons? I mean just like this work in IE/Mac but doesn't work in IE/PC for instance.

login or register to post comments

Re: Browser comparisons

Submitted by Jeff Howden on June 19, 2001 - 09:57.

I've found that with regards to Windows compatibility you can generally count on the compability charts in the documentation for Internet Explorer and Netscape Navigator. However, there's nothing as good as object and method support checking.

I'm concerned about your statement "just like this work in IE/Mac but doesn't work in IE/PC for instance" though. This is simply not true for the majority of versions of IE that are in use. What happens when you click here? Do you get one or two alert() dialogs?

Microsoft's DHTML Reference Section
Microsoft's Scripting Reference
Netscape's JavaScript Guide
Netscape's JavaScript Reference

login or register to post comments

Re: Browser comparisons

Submitted by Dewara on June 20, 2001 - 07:21.

Ok, I got one alert windows. So what your mean is IE in PC is not different in Mac? I asked this 'cause I never design for Mac since I can't afford to buy one. Thanks

login or register to post comments

I never design for Mac

Submitted by MartinB on June 20, 2001 - 09:43.

I think it's a generally sane approach to say that testing is vital (particularly in terms of scripting). But what do you do with users with Macs? Exclude them? Gracefully degrade?

login or register to post comments

It is worse than that.

Submitted by bennydtown on April 28, 2004 - 20:51.

A lot of my Mac Users have complained about a problem with javascript confirm() that is even worse (in this case) than simply not correctly cancelling requests. Apparently, after clicking the cancel button, their http request still goes through, but the response just doesn't render! So, the server still recieves the request, but it is never displayed to the client. Considering the usual uses for confirm(), this can be catostrophic.

The code for the link is: <a>
If you want to see this in context, go here: http://sleuth.hypotheticalsoftware.com/

Then start a case, and then Quit it. You will be presented with a confirm() popup.

login or register to post comments

Tested it and working

Submitted by memeko on October 20, 2006 - 10:17.

<p>On a Mac IE 5.2
<p>Powerbook G4
<p>Mac OS X 10.3.9

login or register to post comments

Tested it and working II

Submitted by memeko on October 20, 2006 - 10:18.

By the way: The "Test support for the confirm() method." link doesn't work! It has no java script code linked to it, just "JavaScript" on the A tag.

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.