2011-08-18 2 views
0

마지막으로 방문한 페이지를 추적하기 위해 쿠키를 확인, 가져오고 설정하고 있습니다. 그리고 이렇게하려면 자바 스크립트 onload를 호출합니다. 문제는이 js를 실행할 때 반복적으로 참조합니다. 마우스 움직임조차도 매우 닮았습니다. 그러나 나는 onload 이외의 어떤 이벤트도 가지지 않고있다.Javascript window.location이 반복적으로 호출됩니다. IE-8

<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> 

위의 코드는이 페이지가 다시 다시 다시로드의 원인 IE-8 파이어 폭스에서 만에 완벽하게 작동합니다 :

여기 내 JS입니다.

답변

3

쿠키를 설정할 때 세미콜론 다음에 공백이 필요합니다. 또한 만료일이 필요합니다. 브라우저를 닫을 때 만료되는 쿠키를 만들려면 만료 날짜 절을 제거하십시오.

function setSessionCookie(c_name){ 
    document.cookie=c_name + "=" + 'testSession'+'; path=/'; 
} 

난 당신이 here에서 기능을 사용하는 것이 좋습니다 :

function createCookie(name,value,days) { 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime()+(days*24*60*60*1000)); 
     var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

function readCookie(name) { 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for(var i=0;i < ca.length;i++) { 
     var c = ca[i]; 
     while (c.charAt(0)==' ') c = c.substring(1,c.length); 
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
    } 
    return null; 
} 

function eraseCookie(name) { 
    createCookie(name,"",-1); 
} 

대신 setSessionCookie(c_name)를 사용

, 당신은 createCookie(c_name, 'testSession');

+0

사실을 사용할 수 있습니다! 나는 노력했다. 그러나 원인이 아닌 것처럼 보입니다. – Rachel

+0

니스 나는 그것을 몰랐다! –

+0

@Rachel : 만료일을 지정하지 않았으므로 즉시 만료되었습니다. –

관련 문제