欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js從Cookies里面取值的簡(jiǎn)單實(shí)現(xiàn)

 更新時(shí)間:2014年06月30日 17:34:54   投稿:whsnow  
遇到一個(gè)Js從Cookies里面取值的需求,Js貌似沒(méi)有現(xiàn)成的方法可以指定Key值獲取Cookie里面對(duì)應(yīng)的值,簡(jiǎn)單實(shí)現(xiàn)如下

工作過(guò)程中遇到一個(gè)Js從Cookies里面取值的需求,Js貌似沒(méi)有現(xiàn)成的方法可以指定Key值獲取Cookie里面對(duì)應(yīng)的值,參閱網(wǎng)上的代碼,簡(jiǎn)單實(shí)現(xiàn)如下:

1. 服務(wù)端代碼,Page_Load里面Cookies寫(xiě)入幾個(gè)值

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication_TestJS 
{ 
public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
Response.Cookies["DONO"].Value = "EDO1406300001"; 
Response.Cookies["DOID"].Value = "ABCDEFG123456"; 
Response.Cookies["DOSOURCE"].Value = "WUWUWUWU"; 
Response.Cookies["DOTYPE"].Value = "2"; 
} 
} 
}


2. 客戶端代碼,頁(yè)面添加按鈕和文本框,用于觸發(fā)和輸出獲取到的值

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication_TestJS._Default" %> 
<html> 
<script language="javascript" type="text/javascript"> 
function GetCookie() 
{ 
/*獲取Cookies里面存放信息 了解其字符串結(jié)構(gòu)*/ 
var Cookies = document.cookie; 
document.getElementById("<%=txtContent.ClientID%>").innerText = Cookies; 

/*處理字符串截取出來(lái)需要的目標(biāo)值*/ 
var target = "DONO" + "="; 
if (document.cookie.length > 0) 
{ 
start = document.cookie.indexOf(target); 
if (start != -1) 
{ 
start += target.length; 
end = document.cookie.indexOf(";", start); 
if (end == -1) end = document.cookie.length; 
} 
} 

/*目標(biāo)值賦值給控件*/ 
document.getElementById("<%=txtTarget.ClientID%>").innerText = document.cookie.substring(start, end); 
} 
</script> 
<head runat="server"> 
<title></title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<asp:Button ID="btnGetReq" runat="server" Text="獲取內(nèi)容" OnClientClick="GetCookie()" /> 
<br /> 
<asp:TextBox ID="txtContent" runat="server" Columns="120"></asp:TextBox> 
<br /> 
<asp:TextBox ID="txtTarget" runat="server" Columns="120"></asp:TextBox> 
</div> 
</form> 
</body> 
</html>


3.執(zhí)行結(jié)果,可以看到Cookies就是如第一個(gè)文本框中存放結(jié)構(gòu),根據(jù)需要截取相應(yīng)字符串即可

相關(guān)文章

最新評(píng)論