How to get server side cookies value from JavaScript. Here I have declare cookies from server side
如何从JavaScript获取服务器端cookie值。在这里,我从服务器端声明了cookie
I want to check whether cookies are null
or not.
我想检查cookie是否为空。
The C# code:
C#代码:
String UserId = usrDT.Rows[0]["user_id"].ToString();
UserDAO.StorageCookies.myCookie = new HttpCookie("myCookie");
UserDAO.StorageCookies.myCookie.Values.Add("userid", UserId);
UserDAO.StorageCookies.myCookie.Expires = DateTime.Now.AddHours(5);
HttpContext.Current.Response.Cookies.Add(UserDAO.StorageCookies.myCookie);
The JavaScript Code
JavaScript代码
$(document).ready(function () {
$(".edit-btn").click(function () {
var cookies = '@HttpContext.Current.Request.Cookies["myCookie"].Value';
alert(cookies);
var v = $(this).data("textval");
var qid = $(this).data("ques-qid");
var getResponce = "http://localhost:1234/Models/answer.aspx?q=" + qid;
if (cookies != "") {
$.ajax({
type: "GET",
url: getResponce,
data: {
"Submit": "EditAnswer",
"ans_id":v
},
success: function (msg) {
var htmlEditor = $find("editAnswer_ctl02");
htmlEditor.set_content(msg);
}
})
} else {
alert("elseCondition");
$('.login-form').modal('show');
}
})
})
3 个解决方案
#1
3
Your JavaScript is able to select cookies
as cookies
stored at clients browser, just use this function
您的JavaScript可以选择cookie作为存储在客户端浏览器中的cookie,只需使用此功能即可
function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
#2
1
You can include this small javascript library (~800 bytes gzipped!). https://github.com/js-cookie/js-cookie It used to be a jquery plugin but it had very few dependencies on jquery so they made it its own stand alone library.
你可以包含这个小的javascript库(约800字节gzip!)。 https://github.com/js-cookie/js-cookie它曾经是一个jquery插件,但它对jquery的依赖性很少,所以他们把它作为自己的独立库。
Read cookie:
读取cookie:
<script src="/path/to/js.cookie.js"></script>
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined
#3
0
If you don't want to use document.cookie , you can use cookie js which will help to get value of any key Please get js from https://github.com/js-cookie/js-cookie and see it's usage.
如果您不想使用document.cookie,您可以使用cookie js来帮助获取任何密钥的值。请从https://github.com/js-cookie/js-cookie获取js并查看它的用法。
#1
3
Your JavaScript is able to select cookies
as cookies
stored at clients browser, just use this function
您的JavaScript可以选择cookie作为存储在客户端浏览器中的cookie,只需使用此功能即可
function getCookie(cname) {
var name = cname + "=";
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);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
#2
1
You can include this small javascript library (~800 bytes gzipped!). https://github.com/js-cookie/js-cookie It used to be a jquery plugin but it had very few dependencies on jquery so they made it its own stand alone library.
你可以包含这个小的javascript库(约800字节gzip!)。 https://github.com/js-cookie/js-cookie它曾经是一个jquery插件,但它对jquery的依赖性很少,所以他们把它作为自己的独立库。
Read cookie:
读取cookie:
<script src="/path/to/js.cookie.js"></script>
Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined
#3
0
If you don't want to use document.cookie , you can use cookie js which will help to get value of any key Please get js from https://github.com/js-cookie/js-cookie and see it's usage.
如果您不想使用document.cookie,您可以使用cookie js来帮助获取任何密钥的值。请从https://github.com/js-cookie/js-cookie获取js并查看它的用法。