I am checking, getting and setting a cookie to trace the last visited page. And to do this I am calling a Javascript onload. Issue is when I execute this js, it refreshes again and again, very much like a even on mouse movement, but I am not having any event other than onload.
Here is my js:
<script type='text/javascript'>
//<![CDATA[
window.onload = function(event){
var currentPage = window.location.href;
var lastVisited = getCookie('udis');
var sessionId= getCookie('udisSession');
if(lastVisited === null || lastVisited === undefined){
setCookie("udis", currentPage, 365);
lastVisited = getCookie('udis');
}
if(sessionId === null || sessionId === undefined){
setSessionCookie('udisSession');
if(lastVisited !== currentPage){
window.location.href = lastVisited;
}
}
setCookie("udis", currentPage, 365);
updateBreadCrumb();
}
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++){
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name) {
return unescape(y);
}
}
}
function setSessionCookie(c_name){
document.cookie=c_name + "=" + 'testSession'+'; expires=; path=/';
}
function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
//]]>
</script>
The above code works flawlessly in Firefox but in IE-8 it’s causing the page to reload again and again.
When setting a cookie, you need a space after the semicolon. You also need an expiration date. If you want to make a cookie that expires when the browser closes, then remove the expiration date clause.
I suggest you use the functions from here:
Instead of using
setSessionCookie(c_name), you could usecreateCookie(c_name, 'testSession');