?

Log in

No account? Create an account
 

javascript click tracker - Web Developer

About javascript click tracker

Previous Entry javascript click tracker May. 17th, 2005 @ 01:45 pm Next Entry
I'm trying to create a javascript click tracker, but I'm having a problem. The setup is very simple so I'll just give the code.

index.html
---------------
<html>
<head>
<script language="JavaScript">
function clk(el) {
(new Image()).src="http://localhost/jsTracker/tracker.php?url="+escape(el.href);
return true;}
</script>
</head>
<body>
<a href="http://google.com" onmousedown="return clk(this)">A Link</a>
</body>
</html>

tracker.php simply inserts the url of the link into mysql.

The problem is very strange. Everything works fine if the files served from my local machine. If I upload it to the server things get weird. It doesn't work with firefox from my office (3 different machines and 3 different versions), but it works with firefox from every other place (home, friends, etc). It works fine with IE and Konqueror. Note: I did change localhost in the js function to the proper value for the server. It works if I go directly to the URL that's in the javascript so it seems that that line isn't exucted by my local firefox browsers. Does anyone have an ideas on this? Is there a free click tracker script that any of you know of? (free as in I can see the code)
Leave a comment
[User Picture Icon]
From:bloc07
Date:May 17th, 2005 09:40 pm (UTC)
(Link)
Ok, I've been working on this a bit and found some interesting things. It seems to work if I add the following for loop after the 'new Image()' line.

for(i = 0; i < 100000; i++){
j = j + 1;
}

I guess this means that the browser doesn't have time to request the image before moving on to the url of the clicked link, but adding this for loop gives it enough time. I've also found another oddity. If I click the link it updates the db, but if I then click the back button and click the link again it doesn't update the db a second time. I have to refresh the index.html page then click for it to update the db a second time.

Are there any javascript experts that can shed some light on this?
From:ex_gigahertz567
Date:May 17th, 2005 10:22 pm (UTC)
(Link)
I'd guess that the second time you're just hitting the local cache instead of making a new request.
[User Picture Icon]
From:bloc07
Date:May 17th, 2005 10:59 pm (UTC)
(Link)
the cached version of which page? Either way I think it should still run the js funtion for the onmousedown event. Am I wrong?
[User Picture Icon]
From:bloc07
Date:May 18th, 2005 06:45 pm (UTC)
(Link)
Your right, it was using the cached version of the image the second time.
(Leave a comment)
Top of Page Powered by LiveJournal.com