2
<a href="javascript:expand()">  and <a href="#" onclick="javascript:expand()">

What's the difference?

I know the href="#" is the more standard way nowadays to do it. My problem is I have a standard dropdown menu that expands/collapses when user clicks on toggle.

If I do href="#" for the code below, whenever someone clicks on expand the page ALWAYS scroll right back to the top which isn't acceptable from a user friendly point.

If I use href="javascript:expand()" when user clicks expand, the page doesn't move and everything is OK.

So will there be any problems if I just use href="javascript:expand()" instead? or how do I fix the href="#" so the page doesn't scroll back to the top whenever user clicks expand.

Thanks.

EDIT: I know this question may have been asked before, but i'm looking at it from my point of view. Im just asking for a suggestion rather than an explanation.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
AnaMaria
  • 3,520
  • 3
  • 19
  • 36
  • Maybe put onclick="return false;" with the href="javascript:expand()" – designtocode Jul 22 '13 at 09:20
  • 5
    possible duplicate of [JavaScript function in href vs. onclick](http://stackoverflow.com/questions/1070760/javascript-function-in-href-vs-onclick) – fsw Jul 22 '13 at 09:20
  • Possible dup http://stackoverflow.com/questions/134845/href-attribute-for-javascript-links-or-javascriptvoid0 – Kees Sonnema Jul 22 '13 at 09:20
  • 1
    Note that you do _not_ need the `javascript:` part in the event attribute. Just say `onclick="expand();"` (or `onclick="expand();return false;"` if you want to prevent the page from scrolling/navigating due to the default `href="#"`). – nnnnnn Jul 22 '13 at 09:30

5 Answers5

3

If your JavaScript onclick event handler returns false, the scrolling won't occur. You can do it like this:

<a href="#" onclick="expand(); return false;">

If your onclick handler does not return false, the href link will be followed. In the case of href="#", that means scroll to the top of the page.

David Pärsson
  • 6,038
  • 2
  • 37
  • 52
3

The best choice is to not use <a> element. Use any semantically appropriate element or just <div> instead.

Roman Pominov
  • 1,403
  • 1
  • 12
  • 17
0

If you want to keep the href="#" you could do

function expand(e) {
  e.preventDefault()
  // ...
}

which prevents the default anchor click behaviour to take place, or you could return false at the end of it.

For the rest, have a look at this question: JavaScript function in href vs. onclick, which I believe is basically what you're asking.

Community
  • 1
  • 1
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • 1
    This wouldn't work unless the inline `onclick` was updated to actually pass the event object into the function. (And still wouldn't work in all browsers, e.g., not in older IE which doesn't support `preventDefault()`.) – nnnnnn Jul 22 '13 at 09:32
  • You're right, there are two choices, either handling the even or returning false, depending on the requirements. – Alberto Zaccagni Jul 22 '13 at 09:37
0

You should try:

<a href="javascript:void(0);" onclick="expand();">Expand</a>
webrama.pl
  • 1,870
  • 1
  • 23
  • 36
-1

Try this, insted of href="#" replace this href="javascript:;"

Jagan Akash
  • 559
  • 5
  • 26