作業プロセス中に、JSがCookieから価値を取得する必要性に遭遇しました。 JSには、Cookieで対応する値を取得する重要な値を指定する既製の方法がないようです。インターネット上のコードを参照してください。単純な実装は次のとおりです。
1。サーバーコード、page_loadのCookieで書かれている値の数
システムの使用。 System.collections.genericを使用しています。 system.webを使用しています。 system.web.uiを使用しています。 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"]。価値= "edo1406300001"; Response.Cookies ["doid"]。value = "abcdefg123456"; Response.Cookies ["dosource"]。value = "wuwuwuwuu"; Response.Cookies ["dotype"]。value = "2"; }}}2。クライアントコード、ページボタンとテキストボックスを追加して、取得した値をトリガーして出力します
<%@ page Language = "c#" autoeventwireup = "true" codebehind = "default.aspx.cs" entens = "webapplication_testjs._default"%> <html> <script言語= "javascript" type = "text/javascript"> function getcookie(){/*cookies intored in string in string in string in string string string string string string string string string document.cookie; document.getElementById( "<%= txtcontent.clientId%>")。innertext = cookies; /*文字列インターセプトの処理に必要なターゲット値*/ varターゲット= "dono" + "="; if(document.cookie.length> 0){start = document.cookie.indexof(ターゲット); if(start!= -1){start += target.length; end = document.cookie.indexof( ";"、start); if(end == -1)end = document.cookie.length; }} /*ターゲット値はコントロールに割り当てられます* / document.getElementById( "<%= txttarget.clientId%>")。innertext = document.cookie.substring(start、end); } </scrip> <head runat = "server"> <title> </head> </head> <body> <form id = "form1" runat = "server"> <div> <asp:button id = "btngetreq" runat = "server" text = "get content" onclientClick = "getCookie()列= "120"> </asp:textbox> <br/> <asp:textbox id = "txttarget" runat = "server" columnss = "120"> </asp:textbox> </div> </form> </body> </html> </html>3.実行結果は、Cookieが最初のテキストボックスに構造を保存するようなものであり、必要に応じて対応する文字列を傍受できることがわかります。