JavaScript cookies settings, read, delete instance code _javascript tips

Source: Internet
Author: User
Tags set cookie setcookie
Just sorted out some of the JavaScript cookies on the operation of the article, found that this article is also good, recommended that everyone together for reference, select the need, the lack of place is mainly on the path of the setting, like friends can be combined.
Copy Code code as follows:

<script>
function Setcookie (name,value)//two parameters, one is the name of the cookie, one is the value
{
var days = 30; This cookie will be saved for 30 days
var exp = new Date (); New Date ("December 31, 9998");
Exp.settime (Exp.gettime () + days*24*60*60*1000);
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}
function GetCookie (name)//Fetch cookies functions
{
var arr = Document.cookie.match (New RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));
if (arr!= null) return unescape (arr[2)); return null;
}
function Delcookie (name)//Delete cookie
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null) document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();
}

Setcookie ("Xiaoqi", "3")
Alert (GetCookie (' Xiaoqi '));
</script>



A very useful JavaScript read-write cookie function
Copy Code code as follows:

function Getcookieval (offset)
Get the value of the cookie after decoding
{
var endstr = documents.cookie.indexOf (";", offset);
if (endstr = = 1)
Endstr = Documents.cookie.length;
Return unescape (documents.cookie.substring (offset, endstr));
}
function Setcookie (name, value)
Set cookie Value
{
var expdate = new Date ();
var argv = setcookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2)? ARGV[2]: null;
var path = (argc > 3)? ARGV[3]: null;
var domain = (argc > 4)? ARGV[4]: null;
var secure = (argc > 5)? ARGV[5]: false;
if (expires!=null) Expdate.settime (Expdate.gettime () + (expires * 1000));
Documents.cookie = name + "=" + Escape (value) + ((expires = null)? "" : ("; Expires= "+ expdate.togmtstring ()))
+ ((path = null)? "" : ("; Path= "+ path") + ((domain = null)? "" : ("; domain= "+ domain)"
+ (Secure = = True)? "; Secure ":" ");
}
function Delcookie (name)
Delete Cookies
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval = GetCookie (name);
Documents.cookie = name + "=" + Cval + "; Expires= "+ exp.togmtstring ();
}
function GetCookie (name)
Get the original value of the cookie
{
var arg = name + "=";
var alen = Arg.length;
var clen = documents.cookie.length;
var i = 0;
while (I < Clen)
{
var j = i + Alen;
if (Documents.cookie.substring (i, j) = arg)
Return Getcookieval (j);
i = Documents.cookie.indexOf ("", I) + 1;
if (i = = 0) break;
}
return null;
}



Copy Code code as follows:

<script language= "JavaScript" >
<!--
function Openpopup () {
Url= "Popup.htm"
window.open ("gonggao.htm", "Gonggao", "Width=260,height=212,left=200,top=0")
}
function Get_cookie (Name) {
var search = Name + "="
var returnvalue = "";
if (Documents.cookie.length > 0) {
offset = documents.cookie.indexOf (search)
if (offset!=-1) {
Offset + = Search.length
End = Documents.cookie.indexOf (";", offset);
if (end = = 1)
end = Documents.cookie.length;
Returnvalue=unescape (documents.cookie.substring (offset, end))
}
}
Return returnvalue;
}
function Helpor_net () {
if (Get_cookie (' popped ') = = ") {
Openpopup ()
Documents.cookie= "Popped=yes"
}
}
Helpor_net ()
-->
</SCRIPT>



If the point is OK, as long as the cookie is not clear, later access will not be prompted, if not the point of determination will be prompted each time. In the JS file, the whole station contains
Copy Code code as follows:

<script language= "JavaScript" >
<!--
var the_cookie = Document.cookie;
var Broken_cookie = The_cookie.split (":");
var the_visiteraccepted = unescape (broken_cookie[1]);
//
if (the_visiteraccepted== "undefined") {
var tmp=confirm (' When and where ' Chinese people. ');
if (Tmp==false) {
Window.close ();
}else{
var the_visiteraccepted = 1;
var The_cookie = "ilovechina=visiteraccepted:" + Escape (the_visiteraccepted);
Document.cookie = The_cookie;
}
}
-->
</SCRIPT>




1. Cookie compatibility issues
The cookie format has 2 different versions, the first version, which we call Cookie version 0, which was originally developed by Netscape and supported by almost all browsers. The newer version, Cookie version 1, is based on the RFC 2109 document. To ensure compatibility, Java provides that the previously mentioned operations involving cookies are for older versions of cookies. The new version of the cookie is not currently supported by the Javax.servlet.http.Cookie package.
2. The contents of the cookie
The character limits for  the contents of the same cookie are different for different cookie versions. In Cookie Version 0, some special characters, such as spaces, brackets, parentheses, equal numbers (=), commas, double quotes, slashes, question marks, @ symbols, colons, and semicolons, cannot be the contents of a cookie. That's why we set the cookie's content to "test_content" in the example.
 Although it is possible to use these characters in the cookie version 1 rule, given that the new version of the cookie specification is still not supported by all browsers, it is safe to try to avoid using these characters in the contents of the cookie.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.