I am trying to do some stuff with cookies. For now i am just trying to read a cookie and display the value in a popup-box using alert(value). The tutorial I am following is : http://www.w3schools.com/js/tryit.asp?filename=tryjs_cookie_username

我正在尝试用饼干做一些事情。现在我只是想读取一个cookie并使用alert(值)在弹出框中显示该值。我关注的教程是:http://www.w3schools.com/js/tryit.asp?filename = tryjs_cookie_username

Somehow this doesn't work when I implement this into my web application. The cookie will be created, but the reading part somehow does not work. When I use Web Developer Toolbar, I can see that a cookie named 'language' is there.

当我将其实现到我的Web应用程序中时,这不起作用。将创建cookie,但阅读部分不知何故不起作用。当我使用Web Developer Toolbar时,我可以看到名为“language”的cookie就在那里。

This is the script block in my Index.aspx

这是我的Index.aspx中的脚本块

function createCookie() {
    var box = document.getElementById("language");
    var value = box.options[box.selectedIndex].value;
    var name = "language";
    var days = 1;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + ";path=/;";
    document.cookie = 'language=en; expires=Thu, 2 Aug 2012 20:47:11 UTC;                                   path=/'
    window.location = 'http://localhost:57688';
}

function readLanguageCookie() {
    var language = getCookie("language");
    if (language != null && language != "") {
        alert("Language is: " + language);
    } else {
        language = "en";
        if (language != null && language != "") {
            setCookie("language", language, 365);
        }
    }
}

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);
        }
    }
}

And this is how I try to execute the method

这就是我尝试执行该方法的方法

<form method="get" action="http://localhost:57688">
<label for="language">hjhj</label>
<select id="language" onchange="readLanguageCookie()">
<option value="en">English</option>
<option value="nl">Dutch</option>
</select>
</form>

Cookie details:

Name    language
Value   en
Host    localhost
Path    /
Secure  No
Expires Thu, 02 Aug 2012 20:47:11 GMT

What am I doing wrong here?

我在这做错了什么?

1 个解决方案

#1


-1

May be the reason is hardcoded date?:

可能是硬编码日期的原因?:

    document.cookie = 'language=en; expires=Thu, 2 Aug 2012 20:47:11 UTC;

更多相关文章

  1. html中页面传递参数不用cookie不用缓存,js方法搞定
  2. TP-link 无线路由器WDS设置方法图解_无线桥接设置 详细出处参考:h
  3. ThinkPHP生成静态页buildHtml方法
  4. 我正在尝试使用带有post方法的AJAX将用户名和密码发送到php文件
  5. html中插入activex控件之后 需要在 网页中实现控件的属性及方法,
  6. iframe调用后台方法通过response返回html代码
  7. js报错:****is not a function 的解决方法
  8. ExtJS的使用方法汇总(4)——拖放以及弹出窗口
  9. ajax提交含有html数据时的处理方法

随机推荐

  1. 【问题解决方案】ImportError: No module
  2. 120行python代码解锁10000分微信跳一跳
  3. 使用特定顺序的ID列表从Django数据库中获
  4. Python3入门(六)——函数式编程
  5. Python中psutil模块的安装
  6. python环境配置问题及解决办法集锦
  7. SQLAlchemy使用笔记--简介
  8. 在seaborn.jointplot中绘制两个分布
  9. Python2基础用法(中文编码,标识符,注释)
  10. 如何在Python + Django中开发消息墙?