9CBS - Document Center - JavaScript Title beginner JavaScript Cookie (Translation) YJGX007 (Translate) Keyword Cookie Sample Source Code Home http://www.codeproject.com/jscript/cookies_intro.asp
Copyright Notice: You can reprint anything, please sure to indicate the original article in hyperlink http://xinyistudio.vicp.net/ and authors information and this statement [Translator Note: Copy the last sample code of this article to the text file In the middle, the rename file is the homepage.htm format file, run in the browser, this article explains this sample code] Introduction ========================= ======================================== What is the problem I want to solve the automatic access http: / / www.thehungersite.com/. This page can restrict you only access it every day (don't forget to click on this link). So far now, the first thing I did every time is that manually choose my bookmark to load this page. Why not create a simple scriptor for it? Because I want it to work in Netscape and IE, I start learning JavaScript. Solve ================================================== ================ Theme ideas are very simple: create a page and test whether this page has been loaded, if not loading, then link to http: // Www.thehungersite.com/, and set this page as the home page of your browser. It is easy to get a page and redirect it. The problem is how to memorize this page has been accessed. Because JavaScript has no file access to file access, it seems that we can only use cookies. Cookies is a size-limited variable, which is associated with a domain name of a server. By default, the Survival of the cookie is emptied when the browser is turned off (not: when you leave this page), but you can use one Script programs change this situation, after the user closes the browser, make the cookies to store, Netscape uses all cookies in the file, and IE stores each cookie. In addition, different browsers will bring some unexpected situations, you must determine if a user closes cookies in its browser settings.
Everything is very good, but now I have not tested it in IE, call example JavaScript statement: cookieExpires = "01-APR-" NLYEAR "GMT"; Document.Cookies = Cookiename "=" cookievalue "; Expires =" cookieExpires; then call Document.Write (Document.cookie); Document.cookie is empty. -------------------------------------------------- -------------------------------------------------- --- After testing and studying the above sample program, you will find: 1. You can't read and display cookies. If you want to see this cookie, you need to specify another string variable as it, as follows: Document.cookie = cookiename "=" cookievalue "; expires =" cookieExpires; myvar = cookiename "=" CookieValue "; Expires =" CookieExpires; Document.write (MyVar); 2. Browser uses different date formats: Netscape ends with "GMT", IE uses "UTC" because it can build one better Date, like this: var expdate = new date () cookieExpires.settime (expdate.gettime () 1 * (24 * 60 * 60 * 1000) // 1 day cookieExpires.togmtstring () When you display the date part Document.write (Expdate.GetyEAR () "
" " " "
" Expdate.Getdate ()); for the date 2000-11-15, displayed in IE 2000 / 10/15, display 100/10/15 under Netscape (Note: Y2K issues that have been determined to be some lower versions) Examples see the following part of the following: if (Platform == "Mac" ) {Lastvisit = Lastvisit - (24 * 60 * 60 * 1000)} But I can't detect it. The date object has a method of getDate and getDay, the second method returns the index number of the weekday.
-------------------------------------------------- -------------------------------------------------- --- Know these, basically no problem (now you can look at homepage.htm) [Translator Note: Copy the last sample code of this article to the text file and save the HTM format, then run] Finally What is not only a dedicated JS script, but if you want to use it on your web page you must minimize the use of different browsers to test it and pay attention to their versions, many scripts contain browsers Type detection and a large amount of if ... ELSE statement to handle this difference.
Sample page Homepage.htm source code ========================================== ====================