I have implemented a mechanism to track the last visited page using cookies in my JavaScript code, triggered by onload
. However, I am facing an issue where the script keeps refreshing repeatedly, almost as if it's triggered by mouse movement even though the only event used is onload
.
Below is the snippet of my JavaScript:
<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 scenario works perfectly on Firefox but unfortunately, it causes constant reloading in IE-8.