Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-13-2011, 09:02 PM   PM User | #16
Energia
New Coder

 
Join Date: Dec 2010
Posts: 99
Thanks: 18
Thanked 0 Times in 0 Posts
Energia is an unknown quantity at this point
Sovled!

Thank you so mutch for your patience and your pedagogical teaching. I learnd so much today and are so happy for that!

The code works perfect!

Energia is offline   Reply With Quote
Old 05-13-2011, 09:09 PM   PM User | #17
kansel
Regular Coder

 
Join Date: Jul 2002
Location: Kansas, USA
Posts: 465
Thanks: 0
Thanked 45 Times in 44 Posts
kansel is on a distinguished road
@Kor: With all due respect (and I'm not doing this to be argumentative, please don't take it as such), that is not correct.

Yes, these two versions indeed act differently because they are coded differently as inline event handlers.
Code:
<a href="http:www.google.com" onclick="preventLinkAction()">click</a>

<a href="http:www.google.com" onclick="return preventLinkAction()">click</a>
The function called in the first version may as well not return anything at all since that return value is never used.

Quote:
Originally Posted by Kor View Post
No. You must ask also for a return within the handler.
Exactly. When the handler is attached from within a script (not inline as above), the return from within the handler executes whether it is assigned by reference or executed within an anonymous wrapper.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Prevent Link</title>
<script type="text/javascript">

function preventLinkAction(){
  return confirm("Follow link?");
}
window.onload = function(){
  document.getElementById("linkA").onclick = function(){return preventLinkAction();};

  document.getElementById("linkB").onclick = preventLinkAction;
}
</script>
</head>
<body>
<a href="http://google.com/" id="linkA">link A</a>
<a href="http://google.com/" id="linkB">link B</a>
</body>
</html>
The two links in the demo above behave identically.

Since there is no problem interpreting the return from the handler, is there a specific reason to wrap the handler in an anonymous function? Again, I'm not asking to be argumentative, I'm wondering if there is an advanced issue (closure, etc.) that I'm missing here.
kansel is offline   Reply With Quote
Old 05-13-2011, 09:34 PM   PM User | #18
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by kansel View Post
Again, I'm not asking to be argumentative, I'm wondering if there is an advanced issue (closure, etc.) that I'm missing here.
Agree. It looks like we were barking at the wrong tree
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Tags
a tag, array, confirm, link, onclick

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:54 AM.


Advertisement
Log in to turn off these ads.