How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

Post by supermike »

If you have jQuery, you can stop your hyperlink dragging and text selecting by doing this:

Code: Select all

$('A').each(function() {
  $(this)
  .css('MozUserSelect','none')
  .bind('selectstart', function() {
    return false;
  })
  .bind('draggesture', function() {
    return false;
  });     
});
Note I've only tested in FF. You may need to make adjustments for IE.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

Post by kaszu »

Thanks for sharing.
Only I can't imagine when it would be useful.
supermike
Forum Contributor
Posts: 193
Joined: Tue Feb 28, 2006 8:30 pm
Location: Somewhere in the Desert, USA

Re: How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

Post by supermike »

kaszu wrote:Thanks for sharing.
Only I can't imagine when it would be useful.
It's useful because it happens to me a lot in FF. Sometimes when I'm clicking something, I end up clicking and dragging by accident. It happens either with a stuck mouse, or a stuck touchpad, etc. What happens is that FF creates a "ghost-like" copy of the text I just clicked, makes it swing out wherever the cursor is, and then when I lift the mouse up, it swings back where it was. It's distracting and ugly. This is part of the browser's normal drag and drop features. I noticed in Gmail that they turned this off on some of the links. So, I set out to figure this out. With enough experimentation, I figured it out.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

Post by Chris Corbyn »

I'd be very careful about where this is used. Tricks like this often lead to usability nightmares since they interfere with often expected behaviour.
gethinw
Forum Newbie
Posts: 16
Joined: Tue Sep 23, 2008 4:02 am

Re: How To Stop Hyperlink Dragging And Text Selecting w/ jQuery

Post by gethinw »

supermike wrote: I noticed in Gmail that they turned this off on some of the links. So, I set out to figure this out. With enough experimentation, I figured it out.
Gmail doesn't actually use this on links, just things which look like links (ie have a 'hand' cursor when you're hovering over them, but aren't 'a' tags).

If it annoys you, I'm sure there's some way to turn it off in firefox preferences (about:config in the address bar), but you shouldn't assume that your users don't like it - I find it's quite helpful if I'm trying to drag a link onto the bookmarks toolbar, for example.
Post Reply