Reputation: 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?
Upvotes: 0
Views: 243
Reputation: 29482
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>
Upvotes: 0
Reputation: 117354
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,
Upvotes: 0
Reputation: 5798
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.
Upvotes: 1
Reputation: 21497
There seems to be some code missing here. How does onclick="click()" call the "out(input)" function?
Upvotes: 0
Reputation: 117354
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();
Upvotes: 3