Home Web Front-end JS Tutorial Simple implementation of js getting values ​​from Cookies_javascript skills

Simple implementation of js getting values ​​from Cookies_javascript skills

May 16, 2016 pm 04:42 PM
cookies value

During the work process, I encountered a need for JS to obtain values ​​from Cookies. It seems that there is no ready-made method for JS to specify the Key value to obtain the corresponding value in Cookie. Please refer to the code on the Internet. The simple implementation is as follows:

1. Server-side code, write several values ​​​​in Cookies in Page_Load

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"; 
} 
} 
}
Copy after login


2. Client code, add buttons and text boxes to the page, used to trigger and output the obtained value

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

/*处理字符串截取出来需要的目标值*/ 
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; 
} 
} 

/*目标值赋值给控件*/ 
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="获取内容" 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>
Copy after login


3. From the execution result, you can see that Cookies is the structure stored in the first text box. You can intercept the corresponding string as needed

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is an MD5 hash value? What is an MD5 hash value? Feb 18, 2024 pm 08:50 PM

What is the MD5 value? In computer science, MD5 (MessageDigestAlgorithm5) is a commonly used hash function used to digest or encrypt messages. It produces a fixed-length 128-bit binary number, usually represented in 32-bit hexadecimal. The MD5 algorithm was designed by Ronald Rivest in 1991. Although the MD5 algorithm is considered no longer secure in the field of cryptography, it is still widely used in data integrity verification and file verification.

PHP value analysis: Detailed explanation of the concept and application of values ​​in PHP PHP value analysis: Detailed explanation of the concept and application of values ​​in PHP Mar 21, 2024 pm 09:06 PM

PHP value analysis: Detailed explanation of the concept and application of value in PHP In PHP programming, value is a very basic and important concept. In this article, we will take a deep dive into the concept of values ​​in PHP and its application in real-world programming. We will analyze in detail basic value types, variables, arrays, objects and constants, etc., and provide specific code examples to help readers better understand and use values ​​in PHP. Basic value types In PHP, the most common basic value types include integer, floating point, string, Boolean and null. These basic

How to solve the problem of Nginx forwarding lost cookies How to solve the problem of Nginx forwarding lost cookies May 15, 2023 pm 09:10 PM

1. Lost Cookies Operation path one: http://localhost:8080/content/requestAction!showMainServiceReqDetail.action path two: http://localhost/content/requestAction!showMainServiceReqDetail.action path three: http://localhost/clp/ requestAction!showMainServiceReqDetail.action path one is direct access, path two is the same as path

How Scrapy uses proxy IP, user agent, and cookies to avoid anti-crawler strategies How Scrapy uses proxy IP, user agent, and cookies to avoid anti-crawler strategies Jun 23, 2023 pm 01:22 PM

With the development of web crawlers, more and more websites and servers are beginning to adopt anti-crawler strategies to prevent data from being maliciously crawled. These strategies include IP blocking, useragent detection, Cookies verification, etc. Without a corresponding response strategy, our crawlers can easily be labeled as malicious and banned. Therefore, in order to avoid this situation, we need to apply policies such as proxy IP, useragent, and cookies in the crawler program of the Scrapy framework.

Detailed instructions for using cookies Detailed instructions for using cookies Feb 22, 2024 pm 12:21 PM

Cookies are a common web technology used to store information about users' personal preferences and behavior on websites. In today's digital age, almost all websites use cookies to provide personalization and a better user experience. This article will introduce the use of cookies in detail to help users better understand and master this technology. First, let's understand the basic concept of cookies. Cookies are small text files stored on the user's browser by the website and contain information about the user's visit to the website.

Explore unaddressable values ​​in Go Explore unaddressable values ​​in Go Mar 25, 2024 am 09:33 AM

In the Go language, some values ​​are not addressable, that is, their memory addresses cannot be obtained. These values ​​include constants, literals, and expressions that cannot be addressed. In this article, we will explore these non-addressable values ​​and understand their characteristics through concrete code examples. First, let's look at some examples of constants. In the Go language, constants are not addressable because their values ​​are determined at compile time and there is no runtime memory address for access. Here is a sample code: packagemaini

Optional class in Java 8: How to filter possibly null values ​​using filter() method Optional class in Java 8: How to filter possibly null values ​​using filter() method Aug 01, 2023 pm 05:27 PM

Optional class in Java8: How to use the filter() method to filter possibly null values ​​In Java8, the Optional class is a very useful tool that allows us to better handle possibly null values ​​and avoid the occurrence of NullPointerException. The Optional class provides many methods to manipulate potential null values, one of the important methods is filter(). The function of the filter() method is that if Option

What method can be used instead of sessionStorage to store temporary data? What method can be used instead of sessionStorage to store temporary data? Jan 13, 2024 am 10:41 AM

How to replace sessionStorage to store temporary data? sessionStorage is a mechanism provided by HTML5 for storing temporary data in the browser. However, if we want to share temporary data between browsers, or want more flexibility in managing data, we may want to consider alternatives to sessionStorage. The following will introduce several ways to replace sessionStorage and provide corresponding code examples. Use localStor

See all articles