本文目錄一覽:
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;
}
希望對你有用
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/127715.html