Javascript 讀取Cookie
今天在論壇上看到文章,就看一下文件來操作
Step 1:我先在畫面放2個Button,一個是產生Cookie,另一個則是接收Cookie
<asp:Button ID="Button1" runat="server" Text="Generate" />
<asp:Button ID="Button2" runat="server" Text="Get" OnClientClick="checkCookie()" />
Step 2:我在Button1 事件產生幾個cookie出來
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Cookies("wxvbbo")("id") = "wxvbbo"
Response.Cookies("wxvbbo")("blog") = "dotblogs"
Response.Cookies("wxvbbo").Expires = DateTime.Now.AddDays(1)
End Sub
Step3:在checkCookie的Script裡撰寫相關的程式
<script language="javascript" type="text/javascript">
function checkCookie()
{
username=getCookie('id');
receive_page=getCookie('blog')
alert(username);
alert(receive_page);
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
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 ""
}
</script>
畫面應該會長的像醬子
reference