我想在cookie中使用未转义的字符(jQuery) [英] I want unescaped characters in my cookie (jquery)

查看:343
本文介绍了我想在cookie中使用未转义的字符(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将href值存储在cookie中,麻烦的是当我这样做cookie时转义了正斜杠,例如

I want to store the href value in a cookie, trouble is when i do the cookie is escaping the forward slashes, so for example

代码是

$.cookie ("mycookie", $link.attr("href"), { path: '/', expires: 7 });

html是

<li><a id="czechrepublic" href="/cz/cz.html">Česká republika</a></li>

当我存储href时,它被存储为

When i store the href it is being stored as

%2Fcz%2Fcz.html

但是我需要将其存储为/cz/cz.html,是否可以在Jquery中将字符转义,我已经在标准javascript cookie教程中看到了,但是我不确定如何使用Jquery cookie插件

But i need it to be stored as /cz/cz.html is there a way of UNescaping characters in Jquery, i have seen this in standard javascript cookie tutorials but i am not sure how to do it with the Jquery cookie plugin

谢谢

推荐答案

当您通过jQuery cookie插件通过$.cookie('mycookie')检索cookie时,它将使用正确的函数decodeURIComponent自动取消转义.

When you retrieve the cookie through the jQuery cookie plugin via $.cookie('mycookie'), it will automatically be unescaped using the correct function which is decodeURIComponent.

如果需要在服务器端对字符串进行解码,则必须使用所使用的任何一种语言来使用URL解码功能.

If you need to decode the string at the server end, you'll have to use the URL-decoding function in whichever language you're using.

如果由于服务器端无法更改为使用编码的cookie而需要使用未编码的cookie,则应忘记jQuery插件并自行设置cookie:

If you need to have an unencoded cookie because the server end can't be changed to use encoded cookies, you should forget the jQuery plugin andjust set the cookie yourself:

document.cookie= 'mycookie='+$link.attr("href");

对于值/cz/cz.html来说可以,但是您不能将许多其他字符存储在cookie中,这就是jQuery逃脱它们的原因.

For the value /cz/cz.html this will be OK, but there are lots of other characters you can't store in a cookie, which is why jQuery escapes them.

这篇关于我想在cookie中使用未转义的字符(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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