Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  180] [ 2]  / answers: 1 / hits: 39872  / 14 Years ago, thu, december 30, 2010, 12:00:00

How to make hyper link <a>Link</a> a double click link: i:e link should open on double click and single click should do nothing.


More From » jquery

 Answers
0

Okay, so, you can do this:


HTML:


<a id='golink' href='gosomewhere.html'>Go Somewhere</a>

JavaScript using jQuery:


jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
});
});

Live copy:




jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
});
});

<a id='golink' href='http://stackoverflow.com' target=_blank>Go Somewhere</a>

<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>




(It doesn't have to be an ID; you can do this with a class or anything else that lets you form a selector that jQuery can process to hook things up.)


If the user has JavaScript disabled, the link will work normally. Crawlers will find the link normally, etc. If a user has JavaScript enabled, the event handlers will get hooked up and it will require a double click.


The above blows away keyboard navigation, though, so then you have to handle that:


jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
}).keydown(function(event) {
switch (event.which) {
case 13: // Enter
case 32: // Space
window.location = this.href;
return false;
}
});
});

Live copy:




jQuery(function($) {
$('#golink').click(function() {
return false;
}).dblclick(function() {
window.location = this.href;
return false;
}).keydown(function(event) {
switch (event.which) {
case 13: // Enter
case 32: // Space
window.location = this.href;
return false;
}
});
});

<a id='golink' href='http://stackoverflow.com' target=_blank>Go Somewhere</a>

<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>




I can't imagine this is good for accessibility, and I bet there are other things not catered for above. Which all feeds into:


But I'd strongly recommend against doing it without a really good use case.


[#94444] Tuesday, December 28, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aileent

Total Points: 556
Total Questions: 107
Total Answers: 101

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
;