View Full Version : Javscript Mouseover Counting

02-08-2007, 12:03 AM

I have page with those key-word ads on it. For example:

"And then reboot your SQL Server and relogin to see the changes"

When you mouse over "SQL Server" you see a pop-up add from an advertiser that does SQL Server performance tuning.

At any rate: When you click the add, my javascript catches and adds +1 to the count of the number of clicks before it open the advertiser's webpage.

This way I can keep track of the number of times it is clicked.

***All this is pretty standard and easily done***

But my curiosity wants me to count the number of times the mouse hover over the word, which I'll compare to the number of clicks (ie: 1080 hovers vs 13 clicks) to get a percentage of mouseovers vs mouseclicks.

Does anyone know the code I can use to do this? Is this possible? Are there any performance issues inherit with this type of javascript that will be getting called ALOT without being able to move to a new page (hence my existing cgi-bin perl script I call for click-counts won't work)

THANKS for any and all help!!!!!!!:D

02-08-2007, 12:36 AM
Lots of different ways to do this. Here is one of em.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style type="text/css">
span { color: red; }
<script type="text/javascript">
var overObj = null;

function doIt() {
var s = "Over: <br/>";

for (var key in overObj) {
s += "\xA0\xA0'" + key + "' - " + overObj[key] + " times<br/>";

var adiv = document.getElementById("adiv");
adiv.innerHTML = s;

function over(o) {


window.onload = function () {
if (!overObj) {
overObj = {};
var t = document.getElementsByTagName("span");
for (var i = 0; i < t.length; i++) {
overObj[t[i].id] = 0;
t[i].onmouseover = function () { over(this); };
<div><span id="how">How</span> are you <span id="doing">doing</span> on this fine <span id="day">day</span>?
<br />
<div id="adiv"> </div>


02-08-2007, 03:19 PM
yes thanks but,this will show the user how many times they've hovered over the object, but how would I find out? This number needs to get saved somehow. And since Javascript cannot access the server (to say change the information in count.txt) It needs to call something else......without moving to a new page.

Any more ideas?

02-08-2007, 04:29 PM
That was just an example how to count mouseovers. Since you are using a cgi script from a form, I'd think the best way to do it would be write the numbers in hidden fields then do your analysis on submit. Or you could use ajax to do it asynchronously.