2014-07-05 37 views
0

我有一個簡單的彈出窗口代碼,只需點擊一下即可打開兩個網站。javascript每隔24小時在窗口打開的代碼不工作的Cookie

現在的問題是,它不能每24小時工作。它每天工作很多次。

我自己的代碼在哪裏出錯?我該如何解決這個問題?

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 setCookie(c_name,value){ 
var exdays= 1; 
var exdate=new Date(); 
exdate.setHours(exdate.getHours() + exdays); 
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); 
document.cookie=c_name + "=" + c_value; 
} 

function checkCookie(){ 
var username1=getCookie("tabligh1"); 
var usernam = "sendshod"; 
if(username1==null){ 
window.open('#','_parent','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1'); 
window.focus(); 
} 
if(username1=="" | username1==null){ 
if(window.open('site adress1','_blank','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1')){ 
window.focus(); 
setCookie("tabligh1",usernam); 
} 
} 
if(username1=="" | username1==null){ 
if(window.open('site adress2','_blank','toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1')){ 
window.focus(); 
setCookie("tabligh1",usernam); 
} 
} 
} 
document.onclick = checkCookie; 
if ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined)) window.onload = checkCookie; 
+0

「它不適用於每24小時」是什麼意思?我不明白問題是什麼。請描述如何重現問題。 – jfriend00

+0

@ jfriend00問題在於窗戶打開很多次,24小時的cookie不會限制打開窗戶的時間,我只想打開一次,但是它每24小時打開很多次。 – Pap

回答

1

setCookie()功能設置cookie從現在開始,從現在過期1個小時,沒有24小時。

正如你可以在這裏看到在您的代碼:

function setCookie(c_name, value) { 
    var exdays = 1; 
    var exdate = new Date(); 
    exdate.setHours(exdate.getHours() + exdays); 
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); 
    document.cookie = c_name + "=" + c_value; 
} 

您的到期時間從當前時間設置爲1小時。也許你打算把時間增加一天,而不是一個小時?

+0

謝謝,這是一個很糟糕的錯誤! – Pap

相關問題