Forum Moderators: open

Message Too Old, No Replies

Making a link return false at the end of a script

To increase site accessibility

         

Jeremy_H

3:38 am on May 30, 2006 (gmt 0)

10+ Year Member



I have a special menu that is displayed when a link is clicked (through script "f").

<a onClick="f();">Menu</a>

I added support for visitors who don't use mice by adding onKeyPress.

<a onClick="f();" onKeyPress="f();">Menu</a>

Now, what I would like is to add support for people who don't use JavaScript by making a separate page with the special menu. To do this, I added an href to the anchor.

<a href="/menu/" onClick="f();" onKeyPress="f();">Menu</a>

What I now need to do is add "return false" to the script. If the user has JavaScript, script F would initiate, return the menu, and prevent the href from loading. If the user does not have JavaScript, script F would not initiate, and they would be taken to a menu on a separate page.

Doing this works fine:

<a href="/menu/" onClick="f();return false;" onKeyPress="f();return false;">Menu</a>

The thing is, I would like to add the return false command to function f, but I can't get that to work.

This is what my original f script looks like:

function f(){var a=document.getElementById("b").style;if(a.visibility=="visible"){a.visibility="hidden";c--}else{a.visibility="visible"}}

If I try the following variations, then the JavaScript menu does not load and the href follows:

function f(){return false;var a=document.getElementById("b").style;if(a.visibility=="visible"){a.visibility="hidden";c--}else{a.visibility="visible"}}

function f(){var a=document.getElementById("b").style;return false;if(a.visibility=="visible"){a.visibility="hidden";c--}else{a.visibility="visible"}}

If I try the following variations, then the JavaScript menu loads, but the href still follows:

function f(){var a=document.getElementById("b").style;if(a.visibility=="visible"){a.visibility="hidden";c--}else{a.visibility="visible"}return false}

function f(){var a=document.getElementById("b").style;if(a.visibility=="visible"){a.visibility="hidden";c--;return false}else{a.visibility="visible";return false}}

Might anybody have any recommendations, short of adding return false to the end of the action every time script f is called?

Thanks

texmex

8:54 pm on May 30, 2006 (gmt 0)

10+ Year Member



if you want to return false from your function that's fine, but you need to pass that through.

This example will never work as the first line of the function is a return statement (thus terminating the function on the first line


function f(){return false;var a=document.getElementById("b").style;if(a.visibility=="visible"){a.visibility="hidden";c--}else{a.visibility="visible"}}

What you could do, is make the "return false;" the very last line of your function.

Within your HTML code, instead of just calling the function, return it's value; ie:

<a onClick="return f();">Menu</a>

Jeremy_H

9:24 pm on May 30, 2006 (gmt 0)

10+ Year Member



Thanks, adding the return to link makes the function work great!