Javascript cookie articles (translation)

zhaozj2021-02-16  65

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 ========================================== ==================== homepage </ title> </ head> <body> <a href = 'http: // Www.thehungersite.com/hmanual redirection <br> </a> <a href="javascript:RestCookie()"> cookie reset </a> <script language = "javascript"> <! - var bvisitedtoday = False; var lastvisit = getCookie ("lastvisit"); if (lastvisit! = null) {Lastvisit = 1 * lastvisit; var lastvisit; var rightnow = new date (); if (Lasthere.GactYear () == 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; returnnescape (document.cookie.substring (offset, endstr));} On 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) ==</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-18531.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="18531" 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.050</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 = '90EgwkwjZKa760UROU2BL47LDF5z4Zn0xXcFOwGAhoHzxnqeWmDqVj2vITg3aRIGPfs4obCDhuDyDEqYgupy5Q_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>