114

On using Google I found that they are using onclick events in anchor tags.

In more option in google header part, it looks like normal a tag, but onclicking it doesn't get redirected but opened a menu. Normally when using

<a href='more.php' onclick='show_more_menu()'>More >>></a>

It usually goes to 'more.php' without firing show_more_menu(), but I have show a menu in that page itself. How to do like google?

Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
smartkid
  • 1,499
  • 3
  • 14
  • 23

7 Answers7

158

If your onclick function returns false the default browser behaviour is cancelled. As such:

<a href='http://www.google.com' onclick='return check()'>check</a>

<script type='text/javascript'>

function check()
{
    return false;
}

</script>

Either way, whether google does it or not isn't of much importance. It's cleaner to bind your onclick functions within javascript - this way you separate your HTML from other code.

Seeta Somagani
  • 776
  • 1
  • 11
  • 31
TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • 5
    1. To be more sure, use a # in href="#" and do the necessary things within the javascript. It is safe to click on that link with # href; the page does leave/reload url. – Bimal Poudel Jan 21 '15 at 07:50
  • 10
    Follow the above advice with caution, as HTML5 rules explicitly state that `href="#"` is supposed to navigate to the top of the page. You can simply add the `href` attibute without content, and get the click behaviour. Assinging `#` as url for real pages (Rather than single-page 100% height apps) is generally a bad idea. – Mike 'Pomax' Kamermans Oct 18 '18 at 21:51
  • Just a note, in the same way you bind a CSS class to an element and then specify the detailed styles in a separate CSS file, it is perfectly good to bind an inline event handler *function* to an element, and then specify the detailed implementation of that function in a separate JS file. Your HTML file is where you declare the structure, appearance, as well as behavior of your webpage. The detailed implementations reside elsewhere. That's the kind of separation you really want. – Sarsaparilla Dec 24 '20 at 04:16
  • can the function chack pass variable? eg check('123') – Han Whiteking Mar 22 '23 at 11:46
65

You can even try below option:

<a href="javascript:show_more_menu();">More >>></a>
sun2
  • 1,118
  • 1
  • 13
  • 17
  • 2
    Warning: If the function returns any explicit value (ex: `return null;`, but not `return;`), this will have unintended consequences in some browsers, such as FireFox. The page will have all content replaced with the returned value in string format (ex: `[object Object]`). – rannmann Nov 02 '16 at 22:52
59

From what I understand you do not want to redirect when the link is clicked.

You can do:

<a href='javascript:;' onclick='show_more_menu();'>More ></a>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Aditya Manohar
  • 2,204
  • 1
  • 17
  • 20
9

Use following code to show menu instead go to href addres

function show_more_menu(e) {
  if( !confirm(`Go to ${e.target.href} ?`) ) e.preventDefault();
}
<a href='more.php' onclick="show_more_menu(event)"> More >>> </a>
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
2

One more solution that prevents default action even if the javascript function returns any value.

<a href="www.any-website.com" onclick='functionToRun();return false;'>
Nandha
  • 192
  • 1
  • 2
  • 11
0
1) Link to work
<a href="#" onClick={this.setActiveTab}>
      ...View Full List
                    </a>
  setActiveTab = (e) => {
    e.preventDefault();
    console.log(e.target);
}
Sandeep Jain
  • 1,019
  • 9
  • 13
  • 1
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. - [From Review](https://stackoverflow.com/review/low-quality-posts/32238228) – Saeed Zhiany Jul 20 '22 at 05:05
0

Not sure if this is what you are looking for but you can recreate an anchor tag with a div adding role="link", this doesn't have the href but at the same time it should be fine from a SEO point of view

<div tabindex="0" role="link" aria-label="Example Link" onclick="show_more_menu()">
  Click to navigate
</div>
Gianmarco
  • 792
  • 2
  • 14
  • 38