views:

29725

answers:

11

Hi, 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?

+4  A: 

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
doesn't work here. On firefox 3.5
Anders Rune Jensen
thanks for pointing that out, neither of the javascript: solutions work. oops!
roryf
+23  A: 

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.

gargantaun
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
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
Sometimes that makes sense, sometimes it doesn't, as always it depends on the situation. We're arguing the same side here ;-)
roryf
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.
gargantaun
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.
gargantaun
+1. Not only is this "right", as gargantuan's later comment suggests, doing it "right" will help eliminate bugs in your page/app.
eyelidlessness
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
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
+17  A: 
$('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
.live() is probably a better method for this.
eyelidlessness
.live() is useless unless he puts the anchors in the DOM via JS (eg: AJAX request)
Bogdan Constantinescu
Why not just `return false;`?
David Murdoch
@David That would be non-jQuery. We can't have that!
RoToRa
@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
It was just a joke concerning to the tendency to (unnecessarily) use jQuery around here ;-)
RoToRa
IMHO i would use href="javascript:void(null);"
andufo
The bigger problem is none of these solutions work with javascript disabled.The # is the best of the available options in my opinion.
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.
Adam Backstrom
+7  A: 

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
Why did this get down-voted?
Horace Loeb
I guess it got down-voted because it feels a bit icky. Doesn't that sort of coding just feel a bit "smoke and mirrors" to you? I like to have reasonably standard HTML with javascript and CSS adding the beauty on top. A link is a link is a link - it isn't a class 'a' span.
Nick Pierpoint
-1: Don't use semantics-free HTML with CSS to fake the visuals of the semantic default behaviors when a semantic and functional equivalent exists in HTML. gargantuan is right.
eyelidlessness
I dont agree with the down votes either. If using a span for capturing events is wrong, then why jQuery in it's official documentation uses a solution similar to this answer? http://docs.jquery.com/Events/bind#typedatafn. Actually, since the links go anywhere in the page, I think using a span is better than an anchor tag. If instead of span he used p, what would you think? I do believe that in this example the class should have a better name instead of "faking an anchor" tough.
GmonC
This is bad, if you have a link it should be an <a> tag. Not a span disguised to be an <a> it makes no sense to do it this way regardless.
meridimus
+2  A: 

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
+3  A: 

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

mohsen
A common mistake is to forget the semicolon in `<a href="javascript:void(0);" ></a>`
John K
+3  A: 

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
A: 

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
A: 

The button solution seems to be the best!

jahja

A: 

Instead you can simply have the href like below:

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

It will not scroll to the top.

A: 
<a href="#nogo">My Link</a>
Dotcream