Forum Moderators: open
<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
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>