46

On my website I use jQuery to hook the events of elements, namely hyperlinks. As these hyperlinks only perform actions on the current page, and do not lead anywhere, I have been putting a href attribute of "#" in:

<a href="#">My Link</a>

However in some browsers this causes the page to scroll right to top which is obviously undesirable behaviour. I've tried using a blank href value, or not including one, but then the mouse does not change to the hand cursor upon hovering.

What should I put in there?

Tom
  • 4,341
  • 9
  • 27
  • 21
  • 6
    Just use CSS to get the hover hand cursor: a {cursor:pointer} and you'll get the hand on all links, regardless. – peirix Apr 06 '09 at 11:12
  • Where are the pseudo-links taking you? Are you scrolling the page, or are you going to other anchors? – cdmckay Apr 06 '09 at 14:46
  • Does this answer your question? [How can I prevent a click on a '#' link from jumping to top of page?](https://stackoverflow.com/questions/3252730/how-can-i-prevent-a-click-on-a-link-from-jumping-to-top-of-page) – starball Aug 29 '23 at 03:42

12 Answers12

51
$('a').click(function (event) 
{ 
     event.preventDefault(); 
     //here you can also do all sort of things 
});

Then you can put in every href whatever you want and jQuery will trigger the preventDefault() method and you will not be redirected to that place.

Bogdan Constantinescu
  • 5,296
  • 4
  • 39
  • 50
  • .live() is probably a better method for this. – eyelidlessness Jul 20 '09 at 17:28
  • 2
    .live() is useless unless he puts the anchors in the DOM via JS (eg: AJAX request) – Bogdan Constantinescu Jul 21 '09 at 13:23
  • 6
    Why not just `return false;`? – David Murdoch May 03 '10 at 12:50
  • @David That would be non-jQuery. We can't have that! – RoToRa May 03 '10 at 12:52
  • 3
    @RoToRa? How is using jQuery's behavior of `return false` on events to prevent the browser's default action and event bubbling non-jQuery? – David Murdoch May 03 '10 at 15:04
  • It was just a joke concerning to the tendency to (unnecessarily) use jQuery around here ;-) – RoToRa May 04 '10 at 08:00
  • IMHO i would use href="javascript:void(null);" – Andres SK May 17 '10 at 17:48
  • The bigger problem is none of these solutions work with javascript disabled. The # is the best of the available options in my opinion. – Sarhanis Jun 10 '10 at 04:23
  • These comments are scary. @Bogdan .live() is not "useless," live prevents you from binding an event to every anchor on the page. @RoTaRa jQuery's .stopPropagation() is "based on DOM3 Events [as specified by the ECMAScript Language Binding](http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html)." @andufo @user7966 `href="javascript:void(null)"` and `href="#"` are *both* bad solutions because they don't degrade for non-JavaScript experiences. – Annika Backstrom Oct 31 '10 at 01:35
  • @AdamBackstrom and all the others: could you please point out to me/us which is "the best" (?) approach, possibly taking the graceful degradation as a measure of quality? TA – superjos Nov 09 '11 at 14:43
  • UPDATE: live now deprecated – Neil Apr 29 '14 at 09:59
39

Those "anchors" that exist solely to provide a click event, but do not actually link to other content, should really be button elements because that's what they really are.

It can be styled like so:

<button style="border:none; background:transparent; cursor: pointer;">Click me</button>

And of course click events can be attached to buttons without worry of the browser jumping to the top, and without adding extraneous javascript such as onclick="return false;" or event.preventDefault() .

talentedmrjones
  • 7,511
  • 1
  • 26
  • 26
38

You should really put a real link in there. I don't want to sound like a pedant, but that's a fairly bad habit to get into. JQuery and Ajax should always be the last thing you implement. If you have a link that goes no-where, you're not doing it right.

I'm not busting your balls, I mean that with all the best intention.

gargantuan
  • 8,888
  • 16
  • 67
  • 108
  • What he should really be doing is injecting the links via JavaScript anyway, in which case I don't see the problem with not having a valid url. – roryf Apr 06 '09 at 12:01
  • 8
    What he really should be doing is pointing the links to the location where the non-javascript equivalent will be executed. Then overriding the action in jQuery. I'm with gargantuan on this... – Tom Wright Apr 06 '09 at 14:21
  • Sometimes that makes sense, sometimes it doesn't, as always it depends on the situation. We're arguing the same side here ;-) – roryf Apr 07 '09 at 09:42
  • 4
    I'll tread very carefully here, since this kind of conversation can very quickly get out of hand. I mean no harm or insult to anyone, I assure you. But with a website, I can't think of any occasion where it would make sense to have a link that goes no-where. – gargantuan Apr 07 '09 at 10:21
  • 3
    I've done it myself and who hasn't, and I've also been bit in the ass a hundred times when a javascript error in an unrelated part of the code brings everything crashing down, or, when pages are slow to load but people are quick to press buttons. So now, I don't do it anymore. – gargantuan Apr 07 '09 at 10:23
  • 1
    +1. Not only is this "right", as gargantuan's later comment suggests, doing it "right" will help eliminate bugs in your page/app. – eyelidlessness Jul 20 '09 at 17:30
  • 2
    Its impossible to fail gracefully if you don't have real links, and it likely that most search engines will never crawl those pages, though some can properly parse JS its not guaranteed. – UnkwnTech Mar 06 '10 at 02:15
  • 2
    Despite better ways by not using links, the author might be in a situation of maintaining or modifying something without the opportunity to redesign it in which case the original question needs a direct answer; of course this answer is good as best practice for general knowledge when starting projects so it's good supporting material in this thread. I do think as a non-direct answer it's overrated. – John K May 03 '10 at 12:34
  • 2
    100% agree with @gargantaun if it is a website... if you are developing a web app... I think it is OK to have those "fake" links. – Romias Mar 08 '12 at 19:26
11

Why use a <a href>? I solve it like this:

<span class='a'>fake link</span>

And style it with:

.a {text-decoration:underline; cursor:pointer;}

You can easily access it with jQuery:

$(".a").click();

bart
  • 14,958
  • 21
  • 75
  • 105
9

Add return false to the end of your click handler, this prevents the browser default handler occurring which attempts to redirect the page:

$('a').click(function() {
// do stuff
return false;
});
roryf
  • 29,592
  • 16
  • 81
  • 103
5

using jquery, you may want to get only to those with a '#'

$('a[href=#]').click(function(){return false;});

if you use the newest jquery (1.3.x), there's no need to bind it again when the page changes:

$('a[href=#]').live('click', function(){return false;});
djspark
  • 180
  • 4
  • 9
5

you shoud use <a href="javascript:void(0)" ></a> instead of <a href="#" ></a>

mohsen
  • 67
  • 1
  • 1
4

Wonder why nobody said it here earlier: to prevent <a href="#"> from scrolling document position to the top, simply use <a href="#/"> instead. That's mere HTML, no JQuery needed. Using event.preventDefault(); is just too much!

Best
  • 71
  • 4
1

I know this is old but wow, there's such an easy solution.

remove the "href" entirely and just add a class that does the following:

.no-href { cursor:pointer: }

And that's it!

Justin
  • 11
  • 1
0

I almost had this problem and it was very deceiving. I am providing an answer in case someone winds up in my same position.

  1. I thought I had this problem
  2. But, I was using return false and javascript:void(0);
  3. Then a distinct difference in problem kept surfacing:
  4. I realized it's not going ALL the way to the top - and my problem zone was near the bottom of the page so this jump was strange and annoying.
  5. I realized I was using fadeIn() [jQuery library], which for a short time my content was display:none
  6. And then my content extended the reach of the page! Causing what looks like a jump!
  7. Using visibility hidden toggles now..

Hope this helps the person stuck with jumps!!

amurrell
  • 2,383
  • 22
  • 26
-1

Instead you can simply have the href like below:

<a href="javascript:;">My Link</a>

It will not scroll to the top.

Pratik Thakkar
  • 395
  • 2
  • 4
  • 16
  • inline JS like this is not great. Better to use preventDefault. Much easier maintenance, and much less of a hack. – Aaron Sep 01 '11 at 01:14
-1
<a href="#nogo">My Link</a>
Annika Backstrom
  • 13,937
  • 6
  • 46
  • 52
  • I don't know why you guys voted this down, but have you ever tried???? This actually works. If there is no element with id "nogo" then the page won't scroll. – user3368506 Aug 08 '17 at 09:51
  • @user3368506 - the answer was very likely downvoted before the edit – McVenco Nov 20 '17 at 14:24