二維Cookie操作(JS和ASP)

來源:互聯網
上載者:User

<html xmlns="http://www.w3.org/1999/xhtml"><br /><head><br /><%<br />if request.form("subfrm")="btnRun" then<br /> '輸出Cookie為cookieName=User=cookieUserValue&IP=cookieIPValue; path=/abc; domain=192.168.18.25<br />response.Cookies("cookieName")("IP") = "cookieIPValue"<br />response.Cookies("cookieName")("User") = "cookieUserValue"<br />end if<br />if request.form("subfrm")="btnGet" then<br /> '向body中輸出<br />response.Write(request.Cookies("cookieName")("IP"))<br />end if<br />%><br /></head><br /><title>無標題頁</title><br /><mce:script language="javascript"><!--</p><p>function GetCookie(name)<br />{<br />m=document.cookie;<br />re1=new RegExp("(?!\w)"+name+"=[^;]+","");<br />re2=new RegExp("^"+name+"=","");<br />try<br />{<br />var a=m.match(re1)[0];<br />}<br />catch(e)<br />{<br />return null;<br />}<br />eval("var o="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":\'")+"'}");<br />return o;<br />}<br />function ReadCookie() { alert(GetCookie("cookieName").IP); }<br />'輸出Cookie為cookieName=IP=cookieValue; path=/abc/; domain=192.168.18.25<br />function WriteCookie() { document.cookie="cookieName"+"=IP="+"cookieValue"; }<br />function ClearCookie() { document.cookie=""; }<br />// --></mce:script><br /></head><br /><body><br /><table border="0" cellpadding="0" cellspacing="0" width="778px"><br /><tr><br /> <td style="height: 100px; width: 778px;" align="center" valign="middle"> Cookie樣本(對IP的操作)</td><br /></tr><br /><tr><br /> <td align="center" style="height: 25px; width: 778px;"><br /><br /> <br /><br /><form action="default.asp" method="post"><br /> <input name="subfrm" type="submit" ID="btnRun" value="btnRun"/><br /> <input name="subfrm" type="submit" ID="btnGet" value="btnGet"/><br /> <input name="subfrm" type="submit" ID="btnClr" value="btnClr"/><br /></form><br /><br /><br /><button type="button" onclick="WriteCookie()"> 用戶端設定Cookie</button><br /> <br /><button type="button" onclick="ReadCookie()"> 用戶端讀取Cookie</button><br /> <br /><button type="button" onclick="ClearCookie()"> 用戶端清除Cookie</button><br /><br /><br /></td><br /></tr><br /></table><br /></form><br /></body><br /></html><br />

    兩者操作方式不同

        由於JavaScript使用字串拼接來操作Cookie,所以並不方便,因而使用JS操作Cookie時可以適當少用二維Cookie,而在ASP中由於二維Cookie可以封裝多個屬性,所以也能帶來一定的方便性

    輸出路徑不同

       
ASP中

輸出的路徑比JS輸出路徑少了一條斜線,實際上傳某些情況之下也會有些許區別:

        如果應用程式指定一個並不包含斜線(/)的路徑,那麼瀏覽器將不會把它當作目錄解釋。相反,它會將cookie提交到任何與指定模式相匹配的路徑中。例如,如果應用程式指定/apps為路徑範圍,那麼除了路徑/apps/以外,瀏覽器還會將它的cookie提交到路徑/apps-test/、 /apps-old/以及它們的所有子目錄中。這種行為可能並不是開發人員所希望見到的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.