A Community of IE7Pro users
You are not logged in.
Hi,
I am trying to distinguish single click and double click on Anchor tags. But it always captures single click it self and never double click. I have used setTimeout, but no use. Here is my sample code. On Single click of Anchor tag, i do some processing, then it goes to new page. When i double click, i need to do different process. I have setTimeOut for singleclick function , but it never calls the function which i set in setTimeOut.. Is it the session getting expired, as soon as anchor tag is clicked, as it opens new page. Is there any solution i can distinguish these two events for AnchorTags.. Please help me out.
document.body.attachEvent("ondblclick",extractAttrs);
document.body.attachEvent("onclick",findTypes);
var Isdblclick = false;
function extractAttrs(){
Isdblclick = true;
............
..................
alert("Extracted Attributes");
}
function TestNew(obj){
// this is single click function
if(Isdblclick == false ){
.....
....
}else{
alert("Caputred by double click... ignore this..");
}
}
function findTypes(){
var Obj = new Object();
Obj.elNode = window.event.srcElement;
if (Obj.elNode.nodeName == 'a'){
setTimeout((function(){TestNew(Obj.elNode);}),5000);
}
}
Offline
You could try something like this, but the delay on single click is annoying:
<script type="text/javascript">
function one(){
this.singleClick = true;
setTimeout(oneContinued,300);
}
function oneContinued(){
if (!this.singleClick)
return;
this.singleClick = false;
// do here what you want on single click
alert(1);
}
function two(){
singleClick = false;
// do here what you want on double click
alert(2);
}
</script>
<a href="javascript:;" onclick="one()" ondblclick="two()">click</a>(I just realized I wrote this code as if it was a website, but it's not difficult to translate it into user scriptyness.
Oh, and I used 'onclick' instead of attaching eventhandlers because I'm lazy.)
Last edited by Protector0ne (02-11-2009 15:09:37)
Offline
Hi,
Thank you very much. It is working. But some times it is unable to capture double click and and taking it as single click. If i reduce timeout parameter for setTimeout, does it help to capture the double click properly for anchor tags..
Offline
I found that reducing the timeout duration only makes the problem worse, as you have less time for the second click. But I suggest you just experiment, and see what works for you.
Personally, I would go for another approach than the single/double click altogether. May I ask what you're going to be using it for?
Offline
Hi,
I have changed the time interval but there is no change, still it is failing for anchor tags, unable to capture double click.
What i am doing is there are some specific anchor tags, where i need to capture the HREFs , other attributes and do processing if i double click it. If it is single click i just get HREF and just allow it go to next page. So i need to differentiate for some of the anchor tags.
As you suggested, you have other approaches for this. Can I have those approaches, so that it works in all the cases.
Thanks a lot for your prompt replies.
Offline
Did you remember to "return false;" after the onclick event, to prevent the loading of the href target?
Alternatively, you could use a mouse click while holding down a keyboard key, instead of doubleclicking.
Offline
Hi,
Yes. I do return false if i dont want to load the page. As sometimes it is unable to capture double click for anchor tags, planning to change the approach to combination of key stroke and single click , as you suggested. With this technique hope will not have problems to distinguish different anchor tags. Thanks for your alternate suggestion.
when there is any click i am identifying anchor tags by getting the tagname of the element,
var evtObj = new Object();
evtObj.elNode = window.event.srcElement;
I use evtObj.elNode.tagName to decide whether it is anchor or some thing else. Some times if there is any img element or div element are embedded in the anchor tag tagName returns 'img' or 'div' instead of 'a'. Is it getting tagName right way of finding anchor tags on click or is any other method to find anchor tags?
Thanks in advance.
Offline
I would just loop through document.links and add event handlers to the onclick event of all elements therein. That way you know for sure you have anchor objects to work with.
Offline