cookie 中“&”号后的字符串损坏 (javascript)

发布于 2024-09-30 19:30:47 字数 1068 浏览 0 评论 0原文

我有一个小问题,我从 cookie 中读取的字符串在 & 符号之后被破坏。例如,字符串“hello & world”将仅显示“hello”。它是一个短代码字符串,使用开关函数转换为更有意义的内容,然后显示在文本框中。 switch 函数工作正常,但显然如果它一开始没有从 cookie 中读取完整的字符串,那么它将无法在 switch 函数中找到短代码。

我目前正在使用以下代码来读取 cookie...

document.example.textfield.value = switchFunction(unescape(coalesce($_GET['example'],readCookie('_cookie'))));

如果您需要我提供更多信息,请告诉我。这是我在这里发表的第一篇文章,如果有任何错误或不清楚的地方,请提前道歉。

感谢您的帮助。

编辑

switchFunction 看起来像这样......

function SwitchFuntion(Code){
    switch(Code){
       case 'text & text, Text' : return 'new meaningful text'; break;
    }
}

等等......

readCookie 函数就像这样......

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

I have a slight problem in that the string I'm reading from a cookie is broken after the ampersand. So for example the string "hello & world" would just display "hello ". It is a string that is a short code, and converted to something more meaningful using a switch function, and then displayed within a text box. The switch function works fine, but obviously if it is not reading the full string from the cookie in the first place, then it won't be able to locate the short code within the switch function.

I am currently using the following code to read the cookie...

document.example.textfield.value = switchFunction(unescape(coalesce($_GET['example'],readCookie('_cookie'))));

If you need me to supply anymore information then please let me know. This is my first post here, so apologies in advance if anything is wrong or unclear.

Thanks For your help.

EDIT

The switchFunction looks like this..

function SwitchFuntion(Code){
    switch(Code){
       case 'text & text, Text' : return 'new meaningful text'; break;
    }
}

etc....

The readCookie function is like this...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹂绝世的画 2024-10-07 19:30:47

我想我在 ASP.NET MVC 应用程序中遇到了类似的问题。

当我将包含&符号的字符串保存到cookie名称/值对时,它实际上被分成一系列名称/值对,

例如尝试保存 ("value","cookiedata123&book=2&page=0") 将导致创建 3 个名称值对 "value"="cookiedata123"; “书”=“2”;和“page”=“0”

我已经解决了这个问题,方法是在写入 cookie 之前对值进行 URL 编码,并在读出后立即对其进行 URL 解码。在 .net 中,调用如下所示:

// Encode
return System.Web.HttpUtility.UrlEncode(cookieData);

// Decode
return System.Web.HttpUtility.UrlDecode(encodedCookieData);

处理任何可能导致问题的 & 符号、等号等。有关 cookie 中允许的字符的信息,请参阅此处的这篇文章。 Cookie 中允许的字符

I think I have run into a similar problem in an ASP.NET MVC app.

When I save a string containing ampersands to a cookie name/value pair it actually gets split into a series of name/value pairs

e.g. attempting to save ("value","cookiedata123&book=2&page=0") will result in 3 name value pairs being created "value"="cookiedata123"; "book"="2"; and "page"="0".

I have resolved this by URL Encoding the value just before writing to the cookie and URL Decoding it as soon as I read it out. In .net the calls look like this:

// Encode
return System.Web.HttpUtility.UrlEncode(cookieData);

// Decode
return System.Web.HttpUtility.UrlDecode(encodedCookieData);

That takes care of any ampersands, equals signs etc that can cause problems. See this post here for info on characters that are allowed in cookies. Allowed characters in cookies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文