views:

72

answers:

5

in a php file:

<script type="text/javascript">
 function click(input){
  img = new Image();
  img.src = '/click.php?id=' +input;
 }
</script>

<a href="http://www.out.com"  target="_blank" onclick="click('IDinmysql')" >outlink</a>

click.php updates mysql after I click this link

it works for chrome,but not for IE,what is the problem?

+3  A: 

Should'nt it be

onclick="out('IDinmysql')"

Furthermore you should append a timestamp to prevent IE from caching.

img.src = '/click.php?t='+new Date().getTime()+'&id=' +input;

Also would be good to make img private

var img = new Image();
Dr.Molle
It still doesnt work in IE
nzh
A: 

There seems to be some code missing here. How does onclick="click()" call the "out(input)" function?

Steven de Salas
I'm sorry,I typed wrong here.Edited
nzh
+1  A: 

Even with the suggested corrections, your code may sometimes work and sometimes fail as two actions are performed at the same time.

Your onclick event asks the browser to load a new image, then the current page is redirected to a new address.

The problem is that the image is loaded asynchronously and depending on the browser, leaving the page that load the image may also cancel all the pending loadings.

You can solve this by changing the href of your link so it calls /click.php?id=IDinmysql&redirect=http://www.out.com (UrlEncode that correctly). Then your PHP script records the click then makes the redirect.

Mart
I also noticed that Google tracks user clicks (not all clicks, mostly when you are identified with your Google account) on search results that way (first an intermediate Google page is called then a redirect occurs), so it probably is the way to go.
Mart
A: 

The real problem(of course the other things have also to be fixed):

Change the name of the function. There is also a native javascript-method called click,

Dr.Molle
`javascript:void(alert(typeof click));` -> undefined.
dev-null-dweller
Try it in Internet-Explorer inside a link or any other element listed in the MSDN-reference: `<a onclick="alert(typeof click)">click me</a>` : you'll see, it is an object. If you try it in the adressbar, it will not work, because of click is not a method of window, it's a method of some elements.
Dr.Molle
Your right. I always thought that it has to be called by `this.click()`
dev-null-dweller
A: 

If you don't want to use redirects, wait for browser to complete request to click.php and then go to outer address. Code below may require some tweaks to be cross-browser, I remember some browsers have problems with .click() method or onreadystatechange() event.

<script type="text/javascript">
function click(input,aObj){
    img = new Image();
    img.onreadystatechange = function(){
        aObj.onclick = function(){};
        aObj.click();
    };
    img.src = '/click.php?id=' +input;
    return false;
}
</script>

<a href="http://www.out.com" target="_blank" onclick="return click('IDinmysql',this)">outlink</a>

Second idea is to open click.php in pop-up with dimensions 1x1, and make it return <body onload="window.close()"></body>

dev-null-dweller
thanks a lot,that works
nzh