Onclick handler firing multiple times when it shouldn't?

R

Robert Oschler

I have a web page that tracks clicks on certain hyperlinks. I am using
attachEvent() to attach to the document onClick handler, for IE browsers.
It works fine, except that for about 1 out of every 6 clicks, I get 2 to 4
click events for a single click. I know this because each time the
hyperlink is clicked, I write a record to a MySQL database. I write the
record to the database by setting the SRC property of an IFRAME on the page,
to a tracking script.

If I look at the database, for 1 out of every 6 clicks, I see 2 to 4 records
for the same click event (occassionally as many as 6 records). They are at
least 1 second and at most 7 seconds apart from each other.

What could be causing this and how can I fix it?

Thanks.
 
M

Michael Winter

On Sat, 30 Oct 2004 13:29:01 -0400, Robert Oschler

[snip]
If I look at the database, for 1 out of every 6 clicks, I see 2 to 4
records for the same click event (occassionally as many as 6 records).
They are at least 1 second and at most 7 seconds apart from each other.

With that much separation, the most logical explanation is that someone's
clicking more than once (though I'm sure you've already ruled that out).
What could be causing this and how can I fix it?

Without seeing a demonstration, it's very difficult to say.

Mike
 
E

Evertjan.

Robert Oschler wrote on 30 okt 2004 in comp.lang.javascript:
What could be causing this and how can I fix it?

A click-only-once should be simple:


var myClick = false;

function clicked(){
if (myClick) return false;
myClick = true;
do something()
return true;
}

............................

onclick = "clicked()"
 
R

Robert Oschler

Evertjan. said:
var myClick = false;

function clicked(){
if (myClick) return false;
myClick = true;
do something()
return true;
}

...........................

onclick = "clicked()"

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Evertjan,

The problem with that approach is that more than one hyperlink is being
tracked. If I read it right, once any hyperlink was clicked, that function
would return false from then on.

Thanks.
 
E

Evertjan.

Robert Oschler wrote on 30 okt 2004 in comp.lang.javascript:
Evertjan,

The problem with that approach is that more than one hyperlink is
being tracked. If I read it right, once any hyperlink was clicked,
that function would return false from then on.

Only the one single hyperlink were you put the string

onclick = "return clicked()"

in. [Do mind the return here!]

=================================

If you want to use it in different hyperlinks you could have different
myClick variables but I would suggest a timeout instead:

==================================

var myClick = false;

function clicked(){
if (myClick) return false;
myClick = true;
setTimeout('myClick = false;',200)
return true;
}


<a onclick="return clicked(xx)" src='xx.html'>xx</a>
<a onclick="return clicked(yy)" src='yy.html'>yy</a>
<a onclick="return clicked(zz)" src='zz.html'>zz</a>

NOT TESTED !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top