试图在javascript中读写cookie [英] Trying to read and write cookies in javascript

查看:50
本文介绍了试图在javascript中读写cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下网页用于读写cookie。它适用于FireFox,但

不适用于Chrome。

 <  < span class =code-leadattribute>!DOCTYPE     html  >  
< html >
< head >
< script >

function setCookie(cname,cvalue,exdays){
< span class =code-keyword> var d = new Date ();
d.setTime(d.getTime()+(exdays * 24 * 60 * 60 * 1000));
var expires = expires = + d.toGMTString();
document .cookie = cname + = + cvalue + ; + expires + ; path = /;
}

function getCookie(cname){
var name = cname + =;
var encodedCookie = decodeURIComponent( document .cookie);
var ca = decodingCookie.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 ;
}

function checkCookie(){
var user = getCookie( username);
if (user!= ){
alert( 再次欢迎 + user);
} else {
user = prompt( 请输入您的姓名: );
if (user!= && user!= null ){
setCookie( username,user, 30 );
}
}
}
< / script >
< / head >
< body onload = checkCookie() >
< / body >
< / html >





我尝试过:



我已经厌倦了通过打印出来调试它有警报。我从W3学校获得了这段代码。我想我需要给它一个存储cookie的路径。



Bob

解决方案

< blockquote>我在浏览器中检查过,我的页面没有设置cookie。还有其他饼干设置。



以下是我的cookies设置:

 Cookies 
允许网站保存和读取cookie数据(推荐)
仅在您退出浏览器之前保留本地数据
阻止第三方cookie





在浏览器中,三个项目旁边都有一个滑动条。对于第一个,滑动条在右侧,它是蓝色。对于接下来的两个,滑动条位于左侧。这些设置是否正确?



Bob


The following web page is suppose to read and write a cookie. It works in FireFox but
not in Chrome.

<!DOCTYPE html>
<html>
<head>
<script>

function setCookie(cname,cvalue,exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 "";
}

function checkCookie() {
    var user=getCookie("username");
    if (user != "") {
        alert("Welcome again " + user);
    } else {
       user = prompt("Please enter your name:","");
       if (user != "" && user != null) {
           setCookie("username", user, 30);
       }
    }
} 
</script>
</head>
<body onload="checkCookie()">
</body>
</html>



What I have tried:

I have tired to debug it by printing things out with alert. I got this code from W3 Schools. I am thinking that I need to give it a path on where to store the cookie.

Bob

解决方案

I checked in the browser and my page is not setting a cookie. There are other cookies being set.

Here are my cookies settings:

Cookies
Allow sites to save and read cookie data (recommended)
Keep local data only until you quit your browser
Block third-party cookies



In the browser there is a slide bar next to each of the three items. For the first one, the slide bar is to the right and it is the color blue. For the next two, the slide bar is to the left. Are these settings right?

Bob


这篇关于试图在javascript中读写cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆