


Detailed explanation of method examples for setting cookies in javascript
The use of js to set cookies
php can set cookies. What are the benefits of using js to set cookies? When operating on the front end, sometimes a lot of parameters need to be passed. At this time, we can splice these parameters to the back of the URL and pass the values. The parameters are received there, but this is quite troublesome. If we put these data in the cookie , in terms of the development efficiency of developers, it will be higher. Take Taobao as an example. It has first-level classification, second-level classification, third-level, and fourth-level. When we are looking for products, there may be many conditions involved. If we combine these It feels much more convenient to put all the conditions in the cookie. I don’t know if Taobao is like this. Taobao's URL is encrypted and very long. It may be that it is rooted at the back of the URL. Without further ado, let’s look at a small example.
Example
##
//取得cookie function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); //把cookie分割成组 for(var i=0;i < ca.length;i++) { var c = ca[i]; //取得字符串 while (c.charAt(0)==' ') { //判断一下字符串有没有前导空格 c = c.substring(1,c.length); //有的话,从第二位开始取 } if (c.indexOf(nameEQ) == 0) { //如果含有我们要的name return unescape(c.substring(nameEQ.length,c.length)); //解码并截取我们要值 } } return false; } //清除cookie function clearCookie(name) { setCookie(name, "", -1); } //设置cookie function setCookie(name, value, seconds) { seconds = seconds || 0; //seconds有值就直接赋值,没有为0,这个根php不一样。 var expires = ""; if (seconds != 0 ) { //设置cookie生存时间 var date = new Date(); date.setTime(date.getTime()+(seconds*1000)); expires = "; expires="+date.toGMTString(); } document.cookie = name+"="+escape(value)+expires+"; path=/"; //转码并赋值 }
Call the above method:
setCookie("test","tank",1800); //设置cookie的值,生存时间半个小时 alert(getCookie('test')); //取得cookie的值,显示tank clearCookie("test"); //删除cookie的值 alert(getCookie('test')); //test对应的cookie值为空,显示为false.就是getCookie最后返的false值。
The above is the detailed content of Detailed explanation of method examples for setting cookies in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

More and more users are starting to upgrade the win11 system. Since each user has different usage habits, many users are still using the ie11 browser. So what should I do if the win11 system cannot use the ie browser? Does windows11 still support ie11? Let’s take a look at the solution. Solution to the problem that win11 cannot use the ie11 browser 1. First, right-click the start menu and select "Command Prompt (Administrator)" to open it. 2. After opening, directly enter "Netshwinsockreset" and press Enter to confirm. 3. After confirmation, enter "netshadvfirewallreset&rdqu

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser
