Try changing onclick to return the result of your function:
echo "<a href='p.php?p=$randString&s=$postCat' onclick='return setYSession();'>";
Or explicitly return false:
echo "<a href='p.php?p=$randString&s=$postCat' onclick='setYSession();return false'>";
Given that your function returns false , either way will stop the default event behavior, i.e. generally stop link navigation. Then in your function, you can add code for navigation after the completion of Ajax:
function setYSession() { var YposValue = window.pageYOffset; $.get('yPosSession.php?yValue=' + YposValue, function() { window.location.href = 'p.php?p=$randString&s=$postCat'; }); return false; }
Ideally, especially if you use jQuery for $.get() , I remove inline onclick and do something like this:
echo "<a href='p.php?p=$randString&s=$postCat' id='myLink'>"; $("#myLink").click(function() { var YposValue = window.pageYOffset, myHref = this.href; $.get('yPosSession.php?yValue=' + YposValue, function() { window.location.href = myHref; }); return false; });
nnnnnn May 23 '12 at 2:04 a.m. 2012-05-23 02:04
source share