Javascript窗口.location一次又一次被IE-8调用

Javascript window.location is getting called again and again IE-8

本文关键字:IE-8 调用 一次又一次 窗口 location Javascript      更新时间:2023-09-26

我正在检查,获取和设置一个cookie来跟踪最后访问的页面。为此,我调用了Javascript的onload函数。问题是,当我执行这个js时,它会一次又一次刷新,就像鼠标移动一样。但我没有任何事件,除了onload。

这是我的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>

以上代码在Firefox中完美无缺,但在IE-8中会导致页面一次又一次地重新加载

这会导致您的页面被重新加载。

window.location.href=lastVisited

你可能应该用if语句检查当前页面是否不相同,然后刷新。比如window.location.href != lastVisited