Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 1st, 2005, 05:22 AM
PC PC is offline
Registered User
 
Join Date: Jun 2005
Location: Roma, , Italy.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Use of return operator.

Hi all.

A very stupid question, but I want to understand (this is the reason why I bougth this book ;)).

In many web pages we find code like this to open a pop-up window (this is a simplified code):

<a href="foo.htm" onclick="window.open('http://.....'); return false;">click here!</a>


Instead of window.open we can fin also a call to a function with window.open inside.
Apart that this is a safe mode to open a pop-up, my question is: why a return false line is used?
I have understood that onclick it's a short form to call a function and a function normally don't return a value or better return undefined.
I have tested that, without return false, we open a new window but preserve the old window with the original link (no window cancelled with something like [object] writen in).
So, why the use of retun false?

Paolo
Reply With Quote
  #2 (permalink)  
Old July 1st, 2005, 09:48 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Hi PC,

The "return false" cancels the user's click and prevents the normal navigation. Suppose you have this:

Code:
<a href="foo.htm" onclick="window.open('bar.htm')">click here!</a>
When you click on this link, two things happen. First, the popup window with bar.htm is opened. Second, the page you clicked on will navigate to foo.htm.

If you add in return false, only the first action (the popup window will take place) because the default behavior, which is to navigate to foo.htm, is cancelled:

Code:
<a href="foo.htm" onclick="window.open('bar.htm'); return false">click here!</a>
The important thing to realize is that the click event fires before the regular navigation takes place.

Hope this helps.


Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
  #3 (permalink)  
Old July 1st, 2005, 10:44 AM
PC PC is offline
Registered User
 
Join Date: Jun 2005
Location: Roma, , Italy.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Nicholas. Thank you for your answer.
Your explanation solve a part of my doubts.
You could think that my level is very low if I ask similar questions but really I'd like to enter inside and not use Javascript only by coping.
So to test if I understand what you say: onclick is an event, when this event happens, User Agent executes JavaScript code before "obey" with the meaning of element.
But why a false value block UA "to execute" element meaning? Because (I try a answer...) it's like to say "yes, we have a click but now you say false so we haven't a click" ?

Where find this kind of information? For example HTML Reccomendation talk about event but only to say that exist. For example I found information about javascript url type in Client Reference Manual of Javascript 1.3 of Netscape and explain briefly but good.

Thank you for your help.





Paolo
Reply With Quote
  #4 (permalink)  
Old July 1st, 2005, 10:54 AM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Paolo,

You're right in all you say. Perhaps to make it a bit clearer: return false says "yes click happened, but don't do what you usually do".

This is the same as saying event.preventDefault() in Mozilla or event.returnValue = false in IE.

Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use like operator thillaiarasu SQL Server 2000 4 April 23rd, 2010 10:47 AM
having problem with Between operator anantdev SQL Language 1 May 26th, 2008 09:14 AM
new line operator in c# MunishBhatia C# 2005 9 December 11th, 2007 03:03 AM
=== operator in c# surendraparashar C# 2005 8 November 8th, 2007 05:14 AM
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 05:51 AM



All times are GMT -4. The time now is 02:36 PM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.