IE7Pro Forum

A Community of IE7Pro users

You are not logged in.

#1 02-11-2009 05:01:21

virg
New Member
Registered: 13-05-2009
Posts: 12

Single Click and double Click for Anchor Tags

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

 

#2 02-11-2009 15:07:23

Protector0ne
Code monkey
From: Netherlands
Registered: 15-09-2007
Posts: 255

Re: Single Click and double Click for Anchor Tags

You could try something like this, but the delay on single click is annoying:

Code:

<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. smile Oh, and I used 'onclick' instead of attaching eventhandlers because I'm lazy.)

Last edited by Protector0ne (02-11-2009 15:09:37)

Offline

 

#3 03-11-2009 06:41:20

virg
New Member
Registered: 13-05-2009
Posts: 12

Re: Single Click and double Click for Anchor Tags

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

 

#4 03-11-2009 16:31:05

Protector0ne
Code monkey
From: Netherlands
Registered: 15-09-2007
Posts: 255

Re: Single Click and double Click for Anchor Tags

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

 

#5 04-11-2009 07:01:34

virg
New Member
Registered: 13-05-2009
Posts: 12

Re: Single Click and double Click for Anchor Tags

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

 

#6 04-11-2009 16:07:44

Protector0ne
Code monkey
From: Netherlands
Registered: 15-09-2007
Posts: 255

Re: Single Click and double Click for Anchor Tags

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

 

#7 05-11-2009 07:24:11

virg
New Member
Registered: 13-05-2009
Posts: 12

Re: Single Click and double Click for Anchor Tags

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

 

#8 05-11-2009 15:16:03

Protector0ne
Code monkey
From: Netherlands
Registered: 15-09-2007
Posts: 255

Re: Single Click and double Click for Anchor Tags

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

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson