js獲取cook參數(js cook)

本文目錄一覽:

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:16
下一篇 2024-10-03 23:16

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • 三星內存條參數用法介紹

    本文將詳細解釋三星內存條上面的各種參數,讓你更好地了解內存條並選擇適合自己的一款。 一、容量大小 容量大小是內存條最基本的參數,一般以GB為單位表示,常見的有2GB、4GB、8GB…

    編程 2025-04-29
  • Python3定義函數參數類型

    Python是一門動態類型語言,不需要在定義變量時顯示的指定變量類型,但是Python3中提供了函數參數類型的聲明功能,在函數定義時明確定義參數類型。在函數的形參後面加上冒號(:)…

    編程 2025-04-29
  • Spring Boot中發GET請求參數的處理

    本文將詳細介紹如何在Spring Boot中處理GET請求參數,並給出完整的代碼示例。 一、Spring Boot的GET請求參數基礎 在Spring Boot中,處理GET請求參…

    編程 2025-04-29
  • Python input參數變量用法介紹

    本文將從多個方面對Python input括號里參數變量進行闡述與詳解,並提供相應的代碼示例。 一、基本介紹 Python input()函數用於獲取用戶輸入。當程序運行到inpu…

    編程 2025-04-29
  • Python Class括號中的參數用法介紹

    本文將對Python中類的括號中的參數進行詳細解析,以幫助初學者熟悉和掌握類的創建以及參數設置。 一、Class的基本定義 在Python中,通過使用關鍵字class來定義類。類包…

    編程 2025-04-29
  • Python函數名稱相同參數不同:多態

    Python是一門面向對象的編程語言,它強烈支持多態性 一、什麼是多態多態是面向對象三大特性中的一種,它指的是:相同的函數名稱可以有不同的實現方式。也就是說,不同的對象調用同名方法…

    編程 2025-04-29
  • Hibernate日誌打印sql參數

    本文將從多個方面介紹如何在Hibernate中打印SQL參數。Hibernate作為一種ORM框架,可以通過打印SQL參數方便開發者調試和優化Hibernate應用。 一、通過配置…

    編程 2025-04-29
  • 全能編程開發工程師必知——DTD、XML、XSD以及DTD參數實體

    本文將從大體介紹DTD、XML以及XSD三大知識點,同時深入探究DTD參數實體的作用及實際應用場景。 一、DTD介紹 DTD是文檔類型定義(Document Type Defini…

    編程 2025-04-29
  • 解析js base64並轉成unit

    本文將從多個方面詳細介紹js中如何解析base64編碼並轉成unit格式。 一、base64編碼解析 在JavaScript中解析base64編碼可以使用atob()函數,它會將b…

    編程 2025-04-29

發表回復

登錄後才能評論