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>
This above code flawlessly in Firefox but in IE-8 it causing page to reload again and again.
This causes your page to be reloaded.
You should probably check with an if statement to see if the current page is not the same and then refresh. Something like
window.location.href != lastVisited