如何访问在JavaScript中在服务器端声明的cookie值? [英] How to access cookie value declared in a server side in javascript?
本文介绍了如何访问在JavaScript中在服务器端声明的cookie值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问在JavaScript中在服务器端声明的cookie值?
How to access cookie value declared in a server side in javascript?
Response.Cookies["StudentCookies"].Value="SOME VALUE"
现在,我想在javascript中访问此Cookies值
请建议
Now i want to access this Cookies value in javascript
Please suggest
推荐答案
使用JavaScript的"document.cookie"属性访问Cookie.
MSDN帮助 [帖子 [
Use the ''document.cookie'' JavaScript property to access the cookie.
MSDN help[^]
This similar post[^] would help you.
例如,您想要在按钮客户端上获取cookie值连按一下:
For example you want to get the cookie value on button client click even:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GetCookie('StudentCookies')" />
在head
标记内使用以下脚本:
Use following script within head
tag:
<script type="text/javascript">
function GetCookieValue(check_name) {
var a_all_cookies = document.cookie.split(';');
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false; // set boolean t/f default f
for (i = 0; i < a_all_cookies.length; i++) {
// now we'll split apart each name=value pair
a_temp_cookie = a_all_cookies[i].split('=');
// and trim left/right whitespace while we're at it
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+
/g,'); // 如果提取的名称与通过的check_name相匹配 如果(cookie_name == check_name){ b_cookie_found = true ; // 我们需要处理cookie没有值但存在(无=符号)的情况: 如果(a_temp_cookie.length> 1 ){ cookie_value = 不转义(a_temp_cookie [ 1 ].replace(/^ \ s + | \ s +
/g, ''); // if the extracted name matches passed check_name if (cookie_name == check_name) { b_cookie_found = true; // we need to handle case where cookie has no value but exists (no = sign, that is): if (a_temp_cookie.length > 1) { cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+
这篇关于如何访问在JavaScript中在服务器端声明的cookie值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文