首页 > 编程知识 正文

js获取cook参数(js cook)

时间:2023-12-11 12:54:42 阅读:314426 作者:MVLC

本文目录一览:

javascript的cookie 小白求教

就是记录一些用户信息,比如你想记录用户的用户名你就可以写入cookie,以后要用时可以读取cookie来获取此信息,一般格式是:写入要写一个写入函数:如

function setCookie(c_name,value,expiredays)

{

var exdate=new Date()

exdate.setDate(exdate.getDate()+expiredays)

document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";

expires="+exdate.toGMTString())

}上面这个函数中的参数存有 cookie 的名称、值以及过期天数

之后,我们要创建另一个函数来检查是否已设置 cookie:

function getCookie(c_name)

{

if (document.cookie.length0)

{

c_start=document.cookie.indexOf(c_name + "=")

if (c_start!=-1)

{

c_start=c_start + c_name.length+1

c_end=document.cookie.indexOf(";",c_start)

if (c_end==-1) c_end=document.cookie.length

return unescape(document.cookie.substring(c_start,c_end))

}

}

return ""

}上面的函数首先会检查 document.cookie 对象中是否存有 cookie。假如 document.cookie 对象存有某些 cookie,那么会继续检查我们指定的 cookie 是否已储存。如果找到了我们要的 cookie,就返回值,否则返回空字符串。

最后,我们要创建一个函数,这个函数的作用是:如果 cookie 已设置,则显示欢迎词,否则显示提示框来要求用户输入名字。

function checkCookie()

{

username=getCookie('username')

if (username!=null username!="")

{alert('Welcome again '+username+'!')}

else

{

username=prompt('Please enter your name:',"")

if (username!=null username!="")

{

setCookie('username',username,365)

}

}

}

这是所有的代码:

html

head

script type="text/javascript"

function getCookie(c_name)

{

if (document.cookie.length0)

{

c_start=document.cookie.indexOf(c_name + "=")

if (c_start!=-1)

{

c_start=c_start + c_name.length+1

c_end=document.cookie.indexOf(";",c_start)

if (c_end==-1) c_end=document.cookie.length

return unescape(document.cookie.substring(c_start,c_end))

}

}

return ""

}

function setCookie(c_name,value,expiredays)

{

var exdate=new Date()

exdate.setDate(exdate.getDate()+expiredays)

document.cookie=c_name+ "=" +escape(value)+

((expiredays==null) ? "" : ";expires="+exdate.toGMTString())

}

function checkCookie()

{

username=getCookie('username')

if (username!=null username!="")

{alert('Welcome again '+username+'!')}

else

{

username=prompt('Please enter your name:',"")

if (username!=null username!="")

{

setCookie('username',username,365)

}

}

}

/script

/head

body onLoad="checkCookie()"

/body

/html

如何获取cookis

你那个有点问题

把document.form_wm.domain.value;; /////怎么有;;

//写入Cookie的函数

var usr = document.form_wm.usr.value;

var domain = document.form_wm.domain.value;

writeCookie(usr, domain)

function writeCookie(name, value, hours)

{

var expire = "";

if(hours != null)

{

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

expire = "; expires=" + expire.toGMTString();

}

document.cookie = name + "=" + escape(value) + expire;

}

希望对你有用

js中如何获取Cookies的值

首先JS设置cookie:

假设在A页面中要保存变量username的值("jack")到cookie中,key值为name,则相应的JS代码为:

document.cookie="name="+username;

JS读取cookie:

var username=document.cookie.split(";")[0].split("=")[1];

function setCookie(name,value)

{

var Days = 30;

var exp = new Date();

exp.setTime(exp.getTime() + Days*24*60*60*1000);

document.cookie = name +"="+ escape (value) +";expires=" + exp.toGMTString();

}

读取cookies

function getCookie(name)

{

var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");

if(arr=document.cookie.match(reg))

return unescape(arr[2]);

else

return null;

}

扩展资料

服务器可以利用Cookies包含信息的任意性来筛选并经常性维护这些信息,以判断在HTTP传输中的状态。Cookies最典型的应用是判定注册用户是否已经登录网站,用户可能会得到提示,是否在下一次进入此网站时保留用户信息以便简化登录手续,这些都是Cookies的功用。

另一个重要应用场合是“购物车”之类处理。用户可能会在一段时间内在同一家网站的不同页面中选择不同的商品,这些信息都会写入Cookies,以便在最后付款时提取信息。

关于js读取cookie

window.onload

=

function

GetCookie()

{

var

CookieStr

=

document.cookie;

//获取你写的cookie【cookie内容如:CookieInfo=Name=GTwebVersion=2.0】

var

GetName

=

CookieStr.indexOf("Name")

+

5;

//获取到cookie中

Name=

的位置

var

mark

=

CookieStr.indexOf("");

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

if

(CookieStr.substring(GetName,

mark)

!=

"GTweb")

{

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

window.location

=

"";

}

}

如何用js读取cookis

你那个有点问题

把document.form_wm.domain.value;; /////怎么有;;

//写入Cookie的函数

var usr = document.form_wm.usr.value;

var domain = document.form_wm.domain.value;

writeCookie(usr, domain)

function writeCookie(name, value, hours)

{

var expire = "";

if(hours != null)

{

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

expire = "; expires=" + expire.toGMTString();

}

document.cookie = name + "=" + escape(value) + expire;

}

希望对你有用

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。