I'm trying to log all links clicked. I'm just working this out so there may be some astoundingly bad methods. Please point out!
It all works nicely though if I use mouseover events as the trigger. If I use click events then the AJAX bit breaks.
Here's the code (mostly):
Adding event listener to all links (firefox tells me errors but it works?!). When I switch from mouseover to click, the whole thing breaks down.
[code=javascript]
function listenAllLinks( ) {
var a= document.getEle mentsByTagName( "a");
for (x in a) {
a[x].addEventListen er("mouseover", exit,true);
}
}
[/code]
Exit function which gets the AJAX object and makes the URL for the PHP script. Then does the AJAX magic bit.
[code=javascript]
function exit() {
xmlHttp=GetXmlH ttpObject();
if (xmlHttp==null) {
//alert ("Browser does not support HTTP Request")
return
}
var script= "exit.php";
var exitPage= escape(this);
var url=script+"?ex it_page="+exitP age+"&sid="+Mat h.random();
xmlHttp.onready statechange=sta teChanged;
xmlHttp.open("G ET",url,true) ;
xmlHttp.send(nu ll);
}
}
function stateChanged() {
if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" ) {
if (xmlHttp.respon seText==0) {
//alert('error');
}
else {
//alert('success' );
}
}
}
function GetXmlHttpObjec t() {
var xmlHttp=null;
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest( );
}
catch (e) { //Internet Explorer
try { xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" ); }
catch (e) { xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP"); }
}
return xmlHttp;
}
[/code]
I'm a bit mystified as to why it's happy with mouseover and not with click. I suspect that it might be that the click loads a new page so maybe the server is cancelling requests that are unfinished and starting with the new ones before my script has recorded the link clicked.
The other oddity is that on my localhost it works but on my live site it doesn't.
Does anyone know how I can fix this or perhaps a slightly different approach?
Thanks in advance!
Henry
Using apache 2, php 5, mysql 5 (local), apache2 php 4.3 mysql4.1 (live)
It all works nicely though if I use mouseover events as the trigger. If I use click events then the AJAX bit breaks.
Here's the code (mostly):
Adding event listener to all links (firefox tells me errors but it works?!). When I switch from mouseover to click, the whole thing breaks down.
[code=javascript]
function listenAllLinks( ) {
var a= document.getEle mentsByTagName( "a");
for (x in a) {
a[x].addEventListen er("mouseover", exit,true);
}
}
[/code]
Exit function which gets the AJAX object and makes the URL for the PHP script. Then does the AJAX magic bit.
[code=javascript]
function exit() {
xmlHttp=GetXmlH ttpObject();
if (xmlHttp==null) {
//alert ("Browser does not support HTTP Request")
return
}
var script= "exit.php";
var exitPage= escape(this);
var url=script+"?ex it_page="+exitP age+"&sid="+Mat h.random();
xmlHttp.onready statechange=sta teChanged;
xmlHttp.open("G ET",url,true) ;
xmlHttp.send(nu ll);
}
}
function stateChanged() {
if (xmlHttp.readyS tate==4 || xmlHttp.readySt ate=="complete" ) {
if (xmlHttp.respon seText==0) {
//alert('error');
}
else {
//alert('success' );
}
}
}
function GetXmlHttpObjec t() {
var xmlHttp=null;
try { // Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest( );
}
catch (e) { //Internet Explorer
try { xmlHttp=new ActiveXObject(" Msxml2.XMLHTTP" ); }
catch (e) { xmlHttp=new ActiveXObject(" Microsoft.XMLHT TP"); }
}
return xmlHttp;
}
[/code]
I'm a bit mystified as to why it's happy with mouseover and not with click. I suspect that it might be that the click loads a new page so maybe the server is cancelling requests that are unfinished and starting with the new ones before my script has recorded the link clicked.
The other oddity is that on my localhost it works but on my live site it doesn't.
Does anyone know how I can fix this or perhaps a slightly different approach?
Thanks in advance!
Henry
Using apache 2, php 5, mysql 5 (local), apache2 php 4.3 mysql4.1 (live)
Comment