Javascript cookie articles (translation)

zhaozj2021-02-16  45

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

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

HomePage </ title> </ head> <body> <a href='http://www.thehungersite.com/'> Manual Redirection <br> </a <a href = "JavaScript: resetcookie ()> cookie reset </A></p> <p><script language = "javascript"> <! - var bvisitedtoday = false;</p> <p>Var lastvisit = getCookie ("lastvisit"); if (Lastvisit! = null) {Lastvisit = 1 * lastvisit; var Lasthere = New Date (lastvisit); var rightnow = new date ();</p> <p>IF (Lasthere.Getyear () == RightNow.getyear () && Lasthere.getMonth () == RightNow.getMonth () && Lasthere.GetDate () == RightNow.getdate () {bvisitedToday = true;}}</p> <p>IF (bvisitedtoday == false) {setlastlastvisitcookie (); window.location = "http://www.thehungersite.com/"} else {//window.location="about:blank "}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24896.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="24896" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.052</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'vTfj8ofbpqRjxgWFl_2BgLlaFYT_2FF4_2FZN8emrL9ROtzyvOVos9z3C1IEUXRaExn1_2B_2BSpYh7Df4MoyMhRQZzqp44w_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>