2016-01-06 94 views
1

嗨,我需要知道的是如何在html web視圖中讀取cookie。我有一個關閉它的旗幟會產生一個cookie,並且這個想法將會轉到另一個有旗幟的頁面,他會讀取cookie以查看用戶是否已經點擊了...html webview中的Cookie android

這是Cookie代碼

function createCookie() { 
    var d = new Date(); 
    d.setTime(d.getTime() + (1*24*60*60*1000)); 
    var expires = "expires="+d.toUTCString(); 
    document.cookie = "cookie=cookie ; " + expires+';path = http://www.pitstop.com.br/'; 

    document.getElementById('banner_id').style.display='none'; 

} 

function getCookie(cname) { 

var name = cname + "="; 
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); 
    if (c.indexOf(name) == 0) return c.substring(name.length, c.length); 
} 
return null; 
} 

function banner_cookie(){ 

    var teste = getCookie('cookie'); 
    if(teste !=null){ 
     alert(teste); 
     document.getElementById('banner_id').style.display='none'; 

    }else{ 
     document.getElementById('banner_id').style.display 

    } 

    } 

回答

0

您有似乎從一廂情願的想法和誤解乾的許多問題。

例如你的道路是錯誤的,你需要一個名字傳遞給cookie的腳本,而不是硬編碼的名字「曲奇」

你需要採取一個經過充分測試的cookie腳本,並正確地使用它。

function getCookie(name) { 
    var start = document.cookie.indexOf(name + "="); 
    var len = start + name.length + 1; 
    if ((!start) && (name != document.cookie.substring(0, name.length))) { 
    return null; 
    } 
    if (start == -1) return null; 
    var end = document.cookie.indexOf(';', len); 
    if (end == -1) end = document.cookie.length; 
    return unescape(document.cookie.substring(len, end)); 
} 

function setCookie(name, value, expires, path, domain, secure) { 
    var today = new Date(); 
    today.setTime(today.getTime()); 
    if (expires) { 
    expires = expires * 1000 * 60 * 60 * 24; 
    } 
    var expires_date = new Date(today.getTime() + (expires)); 
    document.cookie = name + '=' + escape(value) + 
    ((expires) ? ';expires=' + expires_date.toGMTString() : '') + //expires.toGMTString() 
    ((path) ? ';path=' + path : '') + 
    ((domain) ? ';domain=' + domain : '') + 
    ((secure) ? ';secure' : ''); 
} 
function deleteCookie(name, path, domain) { 
    if (getCookie(name)) document.cookie = name + '=' + 
    ((path) ? ';path=' + path : '') + 
    ((domain) ? ';domain=' + domain : '') + 
    ';expires=Thu, 01-Jan-1970 00:00:01 GMT'; 
} 

您的腳本:

function banner_cookie(){ 
    var teste = getCookie('showbanner')=="true"; 
    document.getElementById('banner_id').style.display=teste?"block":"none"; 
} 

,並設置:

setCookie("showbanner","true",14,"/"); // 2 weeks accessible to all website 

,並刪除

deleteCookie("showbanner","/"); 
+1

謝謝。這正是我所需要的 –