2012-02-06 61 views
0

由於某種原因,此腳本卡在重定向循環中,並且不會讓您離開「android.html」。地址欄顯示它正試圖去「的index.html」,但它只是閃爍然後停留在「android.html」重定向cookie卡在循環中

var caution = false 

function setCookie(name, value, expires, path, domain, secure) { 
    var curCookie = name + "=" + escape(value) + 
      ((expires) ? "; expires=" + expires.toGMTString() : "") + 
      ((path) ? "; path=" + path : "") + 
      ((domain) ? "; domain=" + domain : "") + 
      ((secure) ? "; secure" : "") 
    if (!caution || (name + "=" + escape(value)).length <= 4000) 
      document.cookie = curCookie 
    else 
      if (confirm("Cookie exceeds 4KB and will be cut!")) 
        document.cookie = curCookie 
} 

function getCookie(name) { 
    var prefix = name + "=" 
    var cookieStartIndex = document.cookie.indexOf(prefix) 
    if (cookieStartIndex == -1) 
      return null 
    var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length) 
    if (cookieEndIndex == -1) 
      cookieEndIndex = document.cookie.length 
    return unescape(document.cookie.substring(cookieStartIndex + prefix.length,  cookieEndIndex)) 
} 

function deleteCookie(name, path, domain) { 
    if (getCookie(name)) { 
      document.cookie = name + "=" + 
      ((path) ? "; path=" + path : "") + 
      ((domain) ? "; domain=" + domain : "") + 
      "; expires=Thu, 01-Jan-70 00:00:01 GMT" 
    } 
} 

function fixDate(date) { 
    var base = new Date(0) 
    var skew = base.getTime() 
    if (skew > 0) 
      date.setTime(date.getTime() - skew) 
} 

var now = new Date() 
fixDate(now) 
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000) 
var visits = getCookie("indexVisited") 
if (!visits) 
window.location.replace("./android.html"); 
else 
window.location.replace("./index.html"); 
setCookie("indexVisited", visits, now) 
+0

缺少一大堆分號 – 2012-02-06 17:08:41

回答

0

除了丟失了一大堆分號,你的代碼試圖設置cookie後它會重定向到另一個頁面。 Javascript停止執行重定向,所以你的setCookie調用永遠不會被執行。嘗試在重定向之前移動它。

+0

我應該在什麼行移動我的setCookie之前? – Blainer 2012-02-06 17:21:32

+0

之前'如果(!訪問)' – 2012-02-06 17:23:29

+0

這沒有工作,我仍然不能離開android.html – Blainer 2012-02-06 17:27:09