admin管理员组文章数量:1025244
I have a bootstrap website and I stock user information in cookies with the plugin JS-cookie.
Can I show the value of the cookie in the HTML with classic JS/Jquery or do I have to use a framework like Angular ?
EDIT :
I use js-cookie
So my code is, in the head page :
<script>Cookies.set('money', '200', { expires: 7 });</script>
and in the body :
<p> You have <script>Cookies.get('money')</script> dollars </p>
I have a bootstrap website and I stock user information in cookies with the plugin JS-cookie.
Can I show the value of the cookie in the HTML with classic JS/Jquery or do I have to use a framework like Angular ?
EDIT :
I use js-cookie https://github./js-cookie/js-cookie
So my code is, in the head page :
<script>Cookies.set('money', '200', { expires: 7 });</script>
and in the body :
<p> You have <script>Cookies.get('money')</script> dollars </p>
Share
Improve this question
edited Feb 8, 2016 at 23:44
Samuel-Zacharie Faure
asked Feb 8, 2016 at 23:31
Samuel-Zacharie FaureSamuel-Zacharie Faure
1,15213 silver badges30 bronze badges
3
- Have you tried just using a cookie value like any other value that you might use in jQuery? Did that not work? – zero298 Commented Feb 8, 2016 at 23:35
- 2 short answer: yes. unwanted answer: yes, but don't. cookies are for your server, and are sent automatically with every single http request. If you're pretending the cookie is a user setting, do your user profile management properly. Use the cookie for login status only, then consult the user database (via an API or whatever) to get the data you actually want to show on your site. – Mike 'Pomax' Kamermans Commented Feb 8, 2016 at 23:35
- 1 If cookies were only meant for the server there wouldn't be a client-side API for them. Some client-side only apps can use cookies effectively as a semi-persistent storage mechanism. One could also use LocalStorage. – Trevor Commented Feb 8, 2016 at 23:43
2 Answers
Reset to default 3In native JavaScript, you can do the following. Note: this doesn't seem to work on Stackoverflow because of a security restriction, but it does seem to work in CodePen.
var arr = document.cookie.split(';'),
cookies = {};
for(var i=0; i < arr.length; i++){
var parts = arr[i].split('=');
if(parts.length !== 2) continue;
cookies[parts[0].trim()] = parts[1];
}
console.dir(cookies)
document.getElementById('out').textContent = cookies['myCookieKey'];
<div id="out"></div>
Update
Using the JS-Cookie code:
<p>You have <span id="amount"></span> dollars</p>
<script>
document.getElementById('amount').textContent = Cookies.get('money');
</script>
You don't just insert a script in the middle of HTML to get the result of a function. The script has to perform an operation to put a value into the DOM.
<p You have <span id="amount"></span> dollars </p>
<script>
var amount = Cookies.get('money');
document.getElementById('amount').textContent = amount;
</script>
I have a bootstrap website and I stock user information in cookies with the plugin JS-cookie.
Can I show the value of the cookie in the HTML with classic JS/Jquery or do I have to use a framework like Angular ?
EDIT :
I use js-cookie
So my code is, in the head page :
<script>Cookies.set('money', '200', { expires: 7 });</script>
and in the body :
<p> You have <script>Cookies.get('money')</script> dollars </p>
I have a bootstrap website and I stock user information in cookies with the plugin JS-cookie.
Can I show the value of the cookie in the HTML with classic JS/Jquery or do I have to use a framework like Angular ?
EDIT :
I use js-cookie https://github./js-cookie/js-cookie
So my code is, in the head page :
<script>Cookies.set('money', '200', { expires: 7 });</script>
and in the body :
<p> You have <script>Cookies.get('money')</script> dollars </p>
Share
Improve this question
edited Feb 8, 2016 at 23:44
Samuel-Zacharie Faure
asked Feb 8, 2016 at 23:31
Samuel-Zacharie FaureSamuel-Zacharie Faure
1,15213 silver badges30 bronze badges
3
- Have you tried just using a cookie value like any other value that you might use in jQuery? Did that not work? – zero298 Commented Feb 8, 2016 at 23:35
- 2 short answer: yes. unwanted answer: yes, but don't. cookies are for your server, and are sent automatically with every single http request. If you're pretending the cookie is a user setting, do your user profile management properly. Use the cookie for login status only, then consult the user database (via an API or whatever) to get the data you actually want to show on your site. – Mike 'Pomax' Kamermans Commented Feb 8, 2016 at 23:35
- 1 If cookies were only meant for the server there wouldn't be a client-side API for them. Some client-side only apps can use cookies effectively as a semi-persistent storage mechanism. One could also use LocalStorage. – Trevor Commented Feb 8, 2016 at 23:43
2 Answers
Reset to default 3In native JavaScript, you can do the following. Note: this doesn't seem to work on Stackoverflow because of a security restriction, but it does seem to work in CodePen.
var arr = document.cookie.split(';'),
cookies = {};
for(var i=0; i < arr.length; i++){
var parts = arr[i].split('=');
if(parts.length !== 2) continue;
cookies[parts[0].trim()] = parts[1];
}
console.dir(cookies)
document.getElementById('out').textContent = cookies['myCookieKey'];
<div id="out"></div>
Update
Using the JS-Cookie code:
<p>You have <span id="amount"></span> dollars</p>
<script>
document.getElementById('amount').textContent = Cookies.get('money');
</script>
You don't just insert a script in the middle of HTML to get the result of a function. The script has to perform an operation to put a value into the DOM.
<p You have <span id="amount"></span> dollars </p>
<script>
var amount = Cookies.get('money');
document.getElementById('amount').textContent = amount;
</script>
本文标签: jqueryJavascriptCan I display the value of a cookie in HTMLStack Overflow
版权声明:本文标题:jquery - Javascript - Can I display the value of a cookie in HTML? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745613277a2159133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论