It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Partner Sites
Board index Programming JavaScript Forum

help with tree menu

help with tree menu

Postby mcnika on Thu May 22, 2008 8:59 am

Hi all.

This is a code of a tree menu.
I need to know what meens

Code: Select all
onClick="javascript:{show(tr2,2); return false;}


why is needed "return false" and what is javascript:{........

Please reply me as soon as possible.Thanks a lot.

Code: Select all
<html>
<head>
</head>
<body bgcolor="#F2F2F2">
<SCRIPT language=javascript> 
function show(sec, n){
    if (sec.style.display=="none")
sec.style.display="block";
    else
sec.style.display="none"; 

}
</SCRIPT>
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="left">
<A onClick="javascript:{show(tr1,1); return false;}" href=""><STRONG>Image Configurations</STRONG></A>
<DIV id=tr1 style="DISPLAY: none; padding-left:50px;" align=justify>
Upload<br />
View Images<br />
</DIV>
</td>
</tr>
<tr>
<td align="left">
<A onClick="javascript:{show(tr2,2); return false;}" href=""><STRONG>Section</STRONG></A>
<DIV id=tr2 style="DISPLAY: none; padding-left:50px;" align=justify>
Add<br />
View All Section<br />
</DIV>
</td>
</tr>
</table>
</body>
</html>
mcnika
 
Posts: 5
Joined: Thu May 22, 2008 8:19 am

Postby leonard on Thu May 22, 2008 9:32 am

Hi mcnika

Code: Select all
onClick="javascript:{show(tr2,2); return false;}


You can ommit the 'javascript:{}' part. The onclick event will implicitly execute javascirpt.

Code: Select all
show(tr2,2);

This will call the show-function defined above, although the second argument has no influence, and therefore is not needed here.

Code: Select all
return false;

This is needed to suppress the following actions. Here it is a href="" in the <a>-TAG. Why it is an <a>-TAG with a href, I do not know, you could also have a <div>-TAG, then the "return false"-clause would not be nessessary.

cheers
- leonard
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby mcnika on Thu May 22, 2008 9:39 am

When I remove "return false" from the code, the tree doesn't open. So it must be.
But I don't understant
Code: Select all
onClick="javascript:{show(tr2,2); return false;}
how word this part. You have written that is call show function, this I understood, but when comes "return false". We use in the programming return for function yes? But now it is not for function.
mcnika
 
Posts: 5
Joined: Thu May 22, 2008 8:19 am

Postby rangana on Thu May 22, 2008 10:04 am

return false is vital for the code so that it will not observe href attribute.

Removing return false in your code opens the folder to which your page resides ;)

Event handler can return a boolean (true or false), and false means: “don’t take the default action”. Adding return false on our function executes the function and after that the event handler returns false, telling the browser not to take the default action.

For further reading:
http://www.quirksmode.org/js/events_early.html

Also, language is a deprecated attribute you could use:
<script type="text/javascript">

Your code could (also) be written as:
Code: Select all
<A onClick="show(tr2,2);return false" href="">


Side note, you had a number of deprecated attributes :(

Hope that makes sense :)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 6:14 am
Location: Cebu City Philippines

Postby leonard on Thu May 22, 2008 10:06 am

Code: Select all
return false;

This is to exit the onclick-event with false, then the href="" (which reloads the page) will not be executed.

Example:
change your code:

substitute:
Code: Select all
<A onClick="javascript:{show(tr1,1); return false;}" href=""><STRONG>Image Configurations</STRONG></A>

with:
Code: Select all
<div onClick="show(tr1,1);"><STRONG>Image Configurations</STRONG></div>


You will see that the return false is not needed, because the div-tag does not reload the page as the <a href=""> does.

cheers!
- leoanrd
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby rangana on Thu May 22, 2008 10:09 am

leonard wrote:You will see that the return false is not needed, because the div-tag does not reload the page as the <a href=""> does.

cheers!
- leoanrd


Hi Leonard,

Where have you seen (in his code) that the onclick is in the div?..
I'm confused (nothing new), or you were just referring the div as an example. Don't you ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 6:14 am
Location: Cebu City Philippines

Postby leonard on Thu May 22, 2008 10:14 am

Where have you seen (in his code) that the onclick is in the div?..

If you do the substitution I wrote you will have the div-TAG.
I just wanted to give an example on how NOT to use the return false, so that it gets more clear.

cheers!
- leonard[/b]
:%s/^M//
There are 10 kinds of people:
Those who understand binary and those who don't.
User avatar
leonard
100+ Club
 
Posts: 147
Joined: Tue Dec 18, 2007 8:11 am
Location: Switzerland

Postby rangana on Thu May 22, 2008 10:23 am

Ah, makes sense. Just an example.
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 6:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 4 guests