如何使用js来获取cookie的值

如何使用js来获取cookie的值,第1张

<!DOCTYPE HTML>

<html lang="en-US">

<head>

<meta charset="UTF-8">

<meta name="keywords" content="字符串,如何使用,相关内容,编辑部,白菜">

<title>白菜编辑部</title>

<style type="text/css">

</style>

<script type="text/javascript">

function readCookie (name)

{

var cookieValue = "";

var search = name + "=";

if (documentcookielength > 0)

{

offset = documentcookieindexOf (search);

if (offset != -1)

{

offset += searchlength;

end = documentcookieindexOf (";", offset);

if (end == -1)

end = documentcookielength;

cookieValue = unescape (documentcookiesubstring (offset, end))

}

}

return cookieValue;

}

function writeCookie (name, value, hours)

{

var expire = "";

if (hours != null)

{

expire = new Date ((new Date ())getTime () + hours 3600000);

expire = "; expires=" + expiretoGMTString ();

}

documentcookie = name + "=" + escape (value) + expire;

}

writeCookie ("myCookie", "my name", 24);

alert (readCookie ("myCookie"));

</script>

</head>

<body>

</body>

</html>

windowonload

=

function

GetCookie()

{

var

CookieStr

=

documentcookie;

//获取你写的cookiecookie内容如:CookieInfo=Name=GTweb&Version=20

var

GetName

=

CookieStrindexOf("Name")

+

5;

//获取到cookie中

Name=

的位置

var

mark

=

CookieStrindexOf("&");

//获取到cookie中符号的&的位置

if

(CookieStrsubstring(GetName,

mark)

!=

"GTweb")

{

//判断cookie中"Name="和"&"之间的字符串是否等于GTweb,如果不等于则跳转到百度的首页,等于那就没任何 *** 作

windowlocation

=

">

用documentcookie 获取当前域名下的cookie。具体代码如下:

var mycookie = documentcookie

它读出来是一串字符串,包含当前域名下的所有cookie,解析字符串可以得到cookie值

documentcookie这个。

以上就是关于如何使用js来获取cookie的值全部的内容,包括:如何使用js来获取cookie的值、关于js读取cookie、js中如何获取cookies中的键值的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9793760.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存