javascript - Pass a clean cookie variable in Javascript / Vue JS
问题描述
My scenario is this.
I have the following variable:
_s.$cookie.set('videoURL', this.sample_url)
Now, when I looked into Chrome dev tools on Application -> Cookies, this is what I see.
As you may see, there is special characters in between the URLs, probably replacing the "/".
How do you clean this such that is passes a clean URL (eg. http://helloworld.com)?
UPDATE:
After the first answer below, I revised my code. However the same special characters still exist.
my code now is as follows. I'm using Vue JS.
urlEncoded = encodeURIComponent('http://helloworld.com')
decodeURL = decodeURIComponent(urlEncoded)
_s.$cookie.set('videoURL', decodeURL)
UPDATE 2
After passing the cookie variable to class variable, finally saw that the URL is now clean of special characters.
_s.videoURL = _s.$cookie.get('videoURL')
console.log(_s.videoURL)
解决方案
You can't store it as a clean url since it has characters that aren't allowed in a cookie.
An option is to encode / decode the value when write/read the cookie
var url = "http://helloworld.com"
var url_encoded = encodeURIComponent(url)
console.log(url_encoded)
console.log(decodeURIComponent(url_encoded))
Updated after question edit, Vue JS is being used.
Here is two addons for Vue that might solve it for you
And as commented, to cached a video URL across different pages of the website, localStorage
might be more appropriate.
推荐阅读
- postgresql - 为什么返回表的函数比运行实际查询慢得多?
- typescript - 样式检查仅适用于内联,不适用于样式表
- reactjs - Reactjs - Uncaught SyntaxError: Unexpected token <
- python - 如何在 python 中从不同的文件夹中导入具有不同文件扩展名或不同文件类型的文件并运行它们而不会出错?
- php - Bootstrap 3:如何在网格内正确使用表单组(表单)?
- elasticsearch - elasticsearch通过查询删除不释放磁盘空间
- jenkins - Jenkinsfile 不被 build 调用
- django - 在 python 中省略 imprts 以提高性能是否合理?
- c# - 无法创建“MyContext”类型的对象。对于设计时支持的不同模式
- java - 当它是回文时,为什么这个反转的 StringBuilder 不等于原始字符串?