|
|
 |
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

July 1st, 2005, 05:22 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Location: Roma, , Italy.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
__________________
Paolo
|

July 1st, 2005, 09:48 AM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 192
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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/
|

July 1st, 2005, 10:44 AM
|
|
Registered User
|
|
Join Date: Jun 2005
Location: Roma, , Italy.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 1st, 2005, 10:54 AM
|
 |
Wrox Author
|
|
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 192
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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/
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |