2012-02-06 62 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