JS Operation Cookie Daquan

Source: Internet
Author: User
Tags date

<script language=javascript>
function Setcookies (name,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) + "expire*=" + **p.togmtstring ();
}

function GetCookies (name)
{
var arr = Document.cookie.match (New RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));
if (arr!= null) return unescape (arr[2)); return null;
}

function GetCookies ()
{

Document.form1.username.value=getcookies ("Loginusername");
\ Put the form you want to initialize here
}

function Delcookies (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null) document.cookie=name + "=" +cval+ "; expire*=" +**p.togmtstring ();
}

function Submit ()
{
Username=document.form1.username.value;
Setcookies (' Loginusername ', username)
\ Create a cookie, the first name, followed by the value
}
</script>

<body onload= "getcookies ()" >
</body>

/////////////////////////////////////////////////////////////////////

function getexpdate (days, hours, minutes) {

var expdate = new Date ();

if (typeof days = = "Number" && typeof hours = = "Number" &&

typeof hours = = "Number") {

Expdate.setdate (expdate.getdate () + parseint (days));

Expdate.sethours (expdate.gethours () + parseint (hours));

Expdate.setminutes (expdate.getminutes () + parseint (minutes));

return expdate.togmtstring ();

}

}


Utility function called by GetCookie ()

function Getcookieval (offset) {

var endstr = document.cookie.indexOf (";", offset);

if (endstr = = 1) {

Endstr = Document.cookie.length;

}

Return unescape (document.cookie.substring (offset, endstr));

}



primary function to retrieve cookies by name

function GetCookie (name) {

var arg = name + "=";

var alen = Arg.length;

var clen = document.cookie.length;

var i = 0;

while (I < Clen) {

var j = i + Alen;

if (Document.cookie.substring (i, j) = Arg) {

Return Getcookieval (j);

}

i = Document.cookie.indexOf ("", I) + 1;

if (i = = 0) break;

}

Return "";

}



Store cookie value with optional details as needed

function Setcookie (name, value, expires, path, domain, secure) {

Document.cookie = name + "=" + Escape (value) +

((expires)? "; Expires= "+ Expires:" "") +

((path)? "; Path= "+ Path:" "") +

(domain)? "; domain= "+ Domain:" "") +

(secure)? "; Secure ":" ");

}



Remove the cookie by setting ancient expiration date

function Deletecookie (name,path,domain) {

if (GetCookie (name)) {

Document.cookie = name + "=" +

((path)? "; Path= "+ Path:" "") +

(domain)? "; domain= "+ Domain:" "") +

"; Expires=thu, 01-jan-70 00:00:01 GMT ";

}

}

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.