I’m currently using jQuery Cookies plugin, and depending on which side of the page a viewer clicks, a cookie is applied:
<div id="leftClick">Click Me</div>
<div id="rightClick">Click Me</div>
The problem is once the cookie is stored, it doesn’t update without a refresh. So if the viewer clicked on the right element, but before refreshing the page, then followed the left element it would still report right_ despite the cookie having been overwritten with _left, as the page hasn’t refreshed. Is there a way of reading the cookie realtime without a page refresh?
<script type="text/javascript">
var readCookie = $.cookie('whichSide');
$(document).ready(function() {
$('#leftClick').click(function(){
$.cookie('whichSide', 'left_');
});
$('#rightClick').click(function(){
$.cookie('whichSide', 'right_');
});
$('#login').click(function(){
_gaq.push(['_trackEvent', 'viewerChose', readCookie+'side']);
});
});
</script>
This is because you are storing the value of your cookie in the
readCookievariable. This variable will not be automatically updated when you update your cookie which is why you are getting the old value when referring to it.You probably want something like this: