Copyright Notice: You can reprint anything, please be sure to indicate the original source of the article by hyperlink http://xinyistudio.vicp.net/ and author information and this statement
[Translator Note: Copy the last sample code of this article to the text file, the rename file is the homepage.htm format file, run in the browser, this article explains this sample code]
Introduction
============================================================================================================================================================================================================= ================
The problem is what 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
============================================================================================================================================================================================================= ================
The subject idea is very simple: create a page and test whether this page has been loaded, if not loaded, then link it to http://www.thehungersite.com, and set this page as the home page of the 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 =" cookieExpire; then call Document.Write (Document.cookie);
Document.cookie is empty.
-------------------------------------------------- -------------------------------------------------- ---
After testing and studied the above sample program, it will be found:
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. The browser uses a different date format: Netscape ends using "GMT", IE uses "UTC" because it can better build a 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 some document.write (Expdate.GetyEAR () "
" Expdate.getMonth () "
" Expdate.Getdate ()); for the date of 2000-11-15, IE is shown as 2000/10/15, showing 100/10/15 under Netscape (Note: It is determined to be some of the lower version of the Netscape browser Y2K problem)
In the example, it sees 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 into a text file and save the HTM format, then running]
Finally, this 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 The browser type detection and a large number of if ... ELSE statements are handled in this way. Sample page Homepage.htm Source code
============================================================================================================================================================================================================= =============