Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript creates a Welcome cookie

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how JavaScript creates a welcome cookie. I hope you will get something after reading this article. Let's discuss it together.

Create a welcome cookie function setCookie (cname, cvalue, exdays) {var d = new Date (); d.setTime (d.getTime () + (exdays*24*60*60*1000)); var expires= "expires=" + d.toGMTString (); [xss_clean] = cname + "=" + cvalue + ";" + expires; / * [xss_clean] = "username=John Doe Expires=Sun, 31 Dec 2017 12:00:00 UTC "; this function sets cookie by adding the cookie name, cookie value, and expired string. * /} function getCookie (cname) {var name = cname + "="; var ca = [xss_clean] .split (';'); for (var I = 0; I < ca.length; iTunes +) {var c = ca.trim (); if (c.indexOf (name) = = 0) {return c.substring (name.length, c.length);} return "" }} / / check whether cookie already exists, that is, check whether the page has visited function checkCookie () {var user = getCookie ('username'); if (user! = ") {alert (" Welcome "+ user +" visit again ");} else {user = prompt (" Please enter your name: ",") / / return value if the user clicks the cancel button in the prompt box, it returns null. If the user clicks the confirm button, the text currently displayed in the input field is returned. If (user! = "" & user! = null) {setCookie ("username", user, 30); / / the name of the cookie (cname), the value of the cookie (cvalue), and the number of days to know that the cookie expires (exdays). }}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report