Beginners JavaScript Cookie (translation)

Source: Internet
Author: User
Tags date format object getdate version variable window access
cookie|javascript| Beginners [Translator Note: Copy the last sample code in this article to a text file, rename the file as a homepage.htm format file, run in a browser, this article explains the sample code


Brief introduction

====================================

The problem is that I want to resolve the automatic access http://www.thehungersite.com. This page can limit your access to it once every day (don't forget to click on this link).
So far, the first thing I did at the beginning was to manually select my bookmarks to load this page.

Why not create a simple script for it?
Because I wanted it to work the same way under Netscape and IE, I started learning JavaScript.

Solve

====================================

The theme is simple: Create a page and test if today's page has been loaded,
If it is not loaded, it is linked to http://www.thehungersite.com through it and set
This page serves as the homepage of the browser.

Getting the page and redirecting it is easy, and the question is how to remember that the page has been accessed.

Because JavaScript has no access to files, it seems we can only use cookies.

Cookies are a variable with a size limit, which is associated with a server's domain name,
By default, the lifetime of a cookie is emptied when the browser is closed (note: not when you leave
This page, but you can use a script to change the situation,
After the user closes the browser to enable cookies to be stored, Netscape in the file using all the
Cookies, and IE stores each cookie separately. In addition, different browsers bring some intent
Not, you have to determine whether a user has the cookies turned off in its browser settings.

Everything's fine and good, but now I haven't tested it in IE, calling the sample JavaScript statement:
Cookieexpires = "01-apr-" + nlyear + "GMT";
Document.cookies = cookiename + "=" + Cookievalue + "; expires= "+ cookieexpires;

and then call
document.write (Document.cookie);

The Document.cookie is empty.

After experimenting and studying the sample program above, you will find that:

1. You are not able to read and display cookies. If you want to look at this cookie you need to specify another string variable that is the same as it:
Document.cookie = cookiename + "=" + Cookievalue + "; expires= "+ cookieexpires;
MyVar = cookiename + "=" + Cookievalue + "; expires= "+ cookieexpires;
document.write (MyVar);

2. The browser uses a different date format:
Netscape uses "GMT" to end, IE uses "UTC" because it can better build a date, as follows:
var expdate = new Date ()
Cookieexpires.settime (Expdate.gettime () + 1 * (* * * 1000))//+1 Day
Cookieexpires.togmtstring ()

When you show the date part
document.write (Expdate.getyear () + "<br>" + expdate.getmonth () + "<br>" + expdate.getdate ());
For the 2000-11-15 date, shown in IE as 2000/10/15, under Netscape display 100/10/15 (note: Some of the lower versions of the
Netscape browser's Y2K problem)

In the example, you see a section like the following:
if (Platform = = "Mac") {
Lastvisit = lastvisit-(24 * 60 * 60 * 1000)
}
But I can't detect it.

Date objects have getdate and Getday methods, and the second method returns the index number of the week's transit.

----------------------------------------
Knowing this, basically there is no problem (now you can take a look at homepage.htm)

[Translator: Copy the last sample code in this article to a text file and save the HTM format, and then run]

The last thing to say is that this is not only a dedicated JS script, if you want to use it on your Web page you have to minimize the use of different browsers to test it and notice their version, many of the scripting program contains browser type detection and a large number of if...else statements to handle this difference.

Sample page homepage.htm source code

<title>Homepage</title>
<body>
<a href= ' http://www.thehungersite.com/' >manual redirection<br></a>
<a href= "Javascript:resetcookie ()" >cookie reset</a>

<script language= "JavaScript" >
<!--
var bvisitedtoday = false;

var lastvisit = GetCookie ("lastvisit");
if (lastvisit!= null)
{
lastvisit = 1 * lastvisit;
var lasthere = new Date (lastvisit);
var rightnow = new Date ();

if (lasthere.getyear () = = Rightnow.getyear ()
&& lasthere.getmonth () = = Rightnow.getmonth ()
&& lasthere.getdate () = = Rightnow.getdate ())
{
Bvisitedtoday = true;
}
}

if (Bvisitedtoday = = False)
{
Setlastlastvisitcookie ();
window.location= "http://www.thehungersite.com/"
}
Else
{
window.location= "About:blank"
}

function Getcookieval (offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr = = 1)
Endstr = Document.cookie.length;
Return unescape (document.cookie.substring (offset, endstr));
}
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 null;
}
function Setcookie (name, value)
{
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;
Document.cookie = name + "=" + Escape (value) +
((expires = null)? "" : ("; Expires= "+ expires.togmtstring ()) +
((Path = null)? "" : ("; Path= "+ path)" +
(domain = null)? "" : ("; domain= "+ domain)" +
(Secure = = True)? "; Secure ":" ");
}
function Setlastlastvisitcookie ()
{
var rightnow = new Date ();
var expdate = new Date ();
Expdate.settime (Expdate.gettime () + 1 * (24 * 60 * 60 * 1000)); +1 Day
Setcookie ("Lastvisit", Rightnow.gettime (), Expdate, "/");
}
function Resetcookie ()
{
Setcookie ("Lastvisit", 0, NULL, "/");
}
-->
</script>
</body>



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.