时间: 2021-07-31 作者:daque
[翻译注:将正文的结果示例代码正片至文本文献中,改名文献为homepage.htm方法文献,在欣赏器中运转,正文以该示例代码解说
简介 ====================================
题目是我想处置机动考察http://www.thehungersite.com。这个页面不妨控制你每一天只考察它一次(不要忘怀在这链接上点击)。 此刻为止,历次一发端我做的第一件事即是细工采用我的书签加载这个页面。
干什么不为它创造一个大略的剧本步调呢? 由于我想让它在netscape和ie下都不妨同样的处事,我发端进修javascript。
处置
====================================
中心思维很大略:创造一个页面并尝试一下即日这个页面能否已被加载过, 即使没有加载,那就经过它链接到http://www.thehungersite.com,而且树立 这个页面动作欣赏器的网页。
获得页面并列定向是很简单的,题目是怎样回顾这个页面已被考察过。
由于javascript没有文献考察的功效,可见咱们只能运用cookies了。
cookies是一个有巨细控制的变量,它与一个效劳器的域名关系联, 默许情景下cookie的存在期是当欣赏器封闭时被清空(提防:不是当你摆脱 这个页面包车型的士功夫),但不妨用一个剧本步调变换这种情景, 在用户封闭欣赏器后使cookies不妨保存下来,netscape在文献中运用一切的 cookie,而ie辨别保存每个cookie。其余,各别的欣赏器会带来少许预见 不到的情景,你必需决定一个用户在它的欣赏器树立中能否封闭了cookies。
十足都很好也很妙,不过此刻我还未在ie中尝试它,挪用示例javascript语句: cookieexpires = "01-apr-" + nlyear + " gmt"; document.cookies = cookiename + "=" + cookievalue + "; expires=" + cookieexpires;
而后挪用 document.write(document.cookie);
document.cookie是空的。
在考查和接洽了一下上头的示例步调后,会创造:
1。你不不妨读和表露cookie。即使你想看一下这个cookie你须要指定与它沟通的另一个字符串变量,如次: document.cookie = cookiename + "=" + cookievalue + "; expires=" + cookieexpires; myvar = cookiename + "=" + cookievalue + "; expires=" + cookieexpires; document.write(myvar);
2. 欣赏器用了各别的日子方法: netscape运用"gmt"中断,ie运用“utc",这是由于它不妨更好的建立一个日子,象底下如许: var expdate = new date() cookieexpires.settime (expdate.gettime() + 1 * (24 * 60 * 60 * 1000)) //+1 day cookieexpires.togmtstring()
当你表露日子局部 document.write(expdate.getyear() + "<br>" + expdate.getmonth() + "<br>" + expdate.getdate()); 对于2000-11-15的日子,在ie中表露为2000/10/15,在netscape下表露100/10/15(注:已决定是少许较低本子的 netscape 欣赏器的y2k题目)
示例中看到像底下如许的局部: if (platform == "mac") { lastvisit = lastvisit - (24 * 60 * 60 * 1000) } 但我不大概检验和测定它。
日子东西有getdate和getday的本领,第二个本领归来在一周中天的索引号。
---------------------------------------- 领会了那些,基础上就没题目了(此刻你不妨看一下homepage.htm)
[翻译注:将正文的结果示例代码拷至文本文献中并生存htm方法,而后运转]
结果要证明的是,这不只是一个专用的js剧本,即使你想将它用在你的web页面上你必需最小水平的运用各别的欣赏器尝试它并提防它们的本子,很多的剧本步调包括了欣赏器典型检验和测定和洪量的if...else语句,以处置如许各别。
示例页面homepage.htm源代码
<html> <head> <title>homepage</title> </head> <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> </html>