Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Client-based Javascript Hyperlink Counter: Counts User Clicks Realtime

Status
Not open for further replies.

joseph2038

Instructor
Jun 2, 2003
2
SG
Hello,

I hope someone can help me with some coding. I've been searching for a script that will allow me to count user clicks on hyperlinks.

I'm doing a research study on interface design. I would like a script that counts the number of clicks that users take to navigate a menu design. I want this number to show ON SCREEN not to record on some server database. The script does not have to "record" the number of clicks just SHOW them realtime as the user is interacting with the site.

The webpage has two frames (top and bottom). The bottom frame has a timer and hopefully a click (hyperlink) counter. The main frame has the navigation interface. As the user navigates <a href tags the clicks are counted.

I searched the web and couldn't find such a script. I'm not familiar enough with JavaScript to write it myself.

Any leads???

Thanks
 
I would suggest you save the data through your frames using cookies, there are lots of functions for cookies on the web.

Then add an OnClick tag to your <a hrefs and use it to run a function that adds 1 to the write cookies which i suggest you name as a link.

Basicly you need to just create all of the cookies for your links on the start of the page and set them to 0, then whenever a user click a link add 1 to the appropriet cookie.

I think there is another way you can do it, not with cookies, and just save the data on some variables, and then do the same and increase their value by one on each click on a link, and then write it out to the client using the innerHTML property of a div or whatever tag you want.

Hope i helped...
 
Here are three files I've just created which might do what you want. Explaination at the bottom

index.html
----------------------------------
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Frameset//EN&quot; &quot;<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<frameset rows=&quot;*,80&quot; frameborder=&quot;NO&quot; border=&quot;0&quot; framespacing=&quot;0&quot;>
<frame src=&quot;mainWindow.html&quot; name=&quot;mainFrame&quot;>
<frame src=&quot;counter.html&quot; name=&quot;counter&quot; scrolling=&quot;NO&quot; noresize>
</frameset>
<noframes><body>

</body></noframes>
</html>
--------------------------

mainWindow.html
--------------------------
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
function anotherClick()
{
parent.counter.addAnotherClick()
}
</script>
</head>

<body>
<a href=&quot; onClick=&quot;anotherClick()&quot; target=&quot;_blank&quot;><a href=&quot; onClick=&quot;anotherClick()&quot; target=&quot;_blank&quot;>news.bbc.co.uk</a><BR>
<a href=&quot; onClick=&quot;anotherClick()&quot; target=&quot;_blank&quot;></body>
</html>
--------------------------

counter.html
--------------------------
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<script language=&quot;JavaScript&quot;>
var counterVariable = 0


function addAnotherClick()
{
counterVariable = counterVariable + 1
document.counterForm.counter.value = counterVariable
}

function onLoadSet()
{
document.counterForm.counter.value = 0
}


</script>
</head>

<body onLoad=&quot;onLoadSet()&quot;>
<form action=&quot;&quot; method=&quot;get&quot; name=&quot;counterForm&quot;>
<input name=&quot;counter&quot; type=&quot;text&quot; value=&quot;&quot; size=&quot;3&quot; maxlength=&quot;3&quot; >
</form>
</body>
</html>
--------------------------

Right this is a simple little thing. In the bottom frame you will notice that there is a form text input, this is were the counting number will be displayed. Further up in counter.html are two functions. The first function to be called is onLoadSet() in the onload command, this sets the value of the text box to 0.

The top most function is the function that will add one to the counter value when your link in the mainWindow.html is clicked. The function is called from the mainWindow.html by the onClick=&quot;anotherClick()&quot; embedded in to the link. The anotherClick() function simply calls the function in the counter.html.

Sounds complicated but it isn't if I have confused try the code, thats the best thing.

Humble Seeker
The longest journey begins with the smallest step.
 
as if you didn't have enough examples already, here is mine...create these three files, then open linkFrameset.html and click away:

save as linkFrameset.html
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title>link counter</title>
<frameset rows=&quot;75%,*&quot;>
<frame name=&quot;topFrame&quot; src=&quot;linkPage.html&quot;/>
<frame name=&quot;botFrame&quot; src=&quot;linkCounter.html&quot;/>
</frameset>
</head>
</html>

save as linkPage.html
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title></title>
<meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;>
<meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;>
</head>

<body>
<br/><a href=&quot;#&quot;>link</a>
<br/><a href=&quot;#&quot;>link</a>
<br/><a href=&quot;#&quot;>link</a>
</body>
</html>


save as linkCounter.html
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<title></title>
<script type=&quot;text/javascript&quot;>
var secs = 0;
function timer() {
document.getElementById(&quot;secs&quot;).innerHTML = secs++;
}
function writeLinks() {
var l = parent.topFrame.document.links;
var s = &quot;&quot;;
for (var x = 0; x < l.length; x++) {
s += 'Link ' + x + ': <span id=&quot;link_'+x+'&quot;></span><br/>';
}
document.getElementById(&quot;links&quot;).innerHTML = s;
}
function attachListeners() {
var l = parent.topFrame.document.links;
for (var x = 0; x < l.length; x++) {
l[x].onclick = handleClick;
l[x].clicks = 0;
l[x].id = &quot;link_&quot; + x;
}
}
function handleClick() {
document.getElementById(this.id).innerHTML = ++this.clicks;
}
window.onload = function() {
window.setInterval(&quot;timer();&quot;, 1000);
writeLinks();
attachListeners();
}
</script>

<style type=&quot;text/css&quot;>
body {
font:menu;
}
</style>

<meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;>
<meta http-equiv=&quot;Pragma&quot; content=&quot;no-cache&quot;>
</head>

<body>
Seconds Elapsed: <span id=&quot;secs&quot;></span>
<p/>Clicks:
<br/>
<div id=&quot;links&quot;></div>
</body>
</html>



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Hello Jeff and HumbleSeeker,

Thanks for the scripting.

I was able to combine some other code and HumbleSeeker's code to get it working properly. It works fairly well now. Jeff's code was pretty neat too. Very simple and clean.

Thanks for your help:

*******************

If interested here is the code for the counter.html page. In includes client-side realtime hyperlink counter and timer (there is some table formatting here to; sorry for the clutter):

<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>
<title>Footer Frame: Timer / Click Counter</title>

<script language=&quot;JavaScript&quot;>


<!-- Begin Timer
startday = new Date();
clockStart = startday.getTime();
function initStopwatch() {
var myTime = new Date();
var timeNow = myTime.getTime();
var timeDiff = timeNow - clockStart;
this.diffSecs = timeDiff/1000;
return(this.diffSecs);
}
function getSecs() {
var mySecs = initStopwatch();
var mySecs1 = &quot;&quot;+mySecs;
mySecs1= mySecs1.substring(0,mySecs1.indexOf(&quot;.&quot;)) + &quot; s&quot;;
document.forms[0].timespent.value = mySecs1
window.setTimeout('getSecs()',1000);
}
// End Timer-->

<!-- Begin Click Counter

var counterVariable = 0


function addAnotherClick()
{
counterVariable = counterVariable + 1
document.counterForm.counter.value = counterVariable
}

function onLoadSet()
{
document.counterForm.counter.value = 0
}

// End Click Counter-->

</script>
<base target=&quot;main&quot;>
</head>



<body bgcolor=&quot;#C1E0FF&quot; onLoad=&quot;window.setTimeout('getSecs()',1);onLoadSet()&quot;>
<div align=&quot;left&quot;>

<FORM action=&quot;&quot; method=&quot;get&quot; name=&quot;counterForm&quot;>
<table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
<tr>
<td width=&quot;33%&quot;><b><font face=&quot;Arial&quot; color=&quot;#FFFFFF&quot; size=&quot;3&quot;>clicks<br>
</font></b>
<input name=&quot;counter&quot; type=&quot;text&quot; value=&quot;&quot; size=&quot;5&quot; maxlength=&quot;3&quot; >

</td>
<td width=&quot;33%&quot;></td>
<td width=&quot;34%&quot;>


<p align=&quot;right&quot;>&nbsp;<b><font face=&quot;Arial&quot; color=&quot;#FFFFFF&quot; size=&quot;3&quot;>secs<br>
</font></b><input size=3 name=timespent style=&quot;font-size: 10pt; font-family: Arial&quot;></p>



</td>
</tr>
</table>
</form>

</div>
</body>
</html>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top