|
更新通知的例子: <!-- var whatsNew = open('','_blank','top=50,left=50,width=200,height=300,' + 'menubar=no,toolbar=no,directories=no,location=no,' + 'status=no,resizable=no,scrollbars=yes'); whatsNew.document.write('<center><b>更新通知</b></center>'); whatsNew.document.write('<p>最后更新日期:00.08.01'); whatsNew.document.write('<p>00.08.01:增加了“我的最爱”栏目。'); whatsNew.document.write('<p align="right">' + '<a href="javascript:self.close()">关闭窗口</a>'); whatsNew.document.close(); -->
预读图像的 JavaScript 例子 var imagePreload = new Image(); imagePreload.src = '001.gif'; imagePreload.src = '002.gif'; imagePreload.src = '003.gif'; 以上例子适合预读少量图片。 function imagePreload() { var imgPreload = new Image(); for (i = 0; i < arguments.length; i++) { imgPreload.src = arguments; } } imagePreload('001.gif', '002.gif', '003.gif', '004.gif', '005.gif'); 以上例子适合预读大量图片。
自定义构造函数 我们已经知道,Array(),Image()等构造函数能让我们构造一个变量。其实我们自己也可以写自己的构造函数。自定义构造函数也是用 function。在 function 里边用 this 来定义属性。
function <构造函数名> [(<参数>)] { ... this.<属性名> = <初始值>; ... }
然后,用 new 构造函数关键字来构造变量:
var <变量名> = new <构造函数名>[(<参数>)];
构造变量以后,<变量名>成为一个对象,它有它自己的属性——用 this 在 function 里设定的属性。 以下是一个从网上找到的搜集浏览器详细资料的自定义构造函数的例子:
function Is() { var agent = navigator.userAgent.toLowerCase(); this.major = parseInt(navigator.appVersion); //主版本号 this.minor = parseFloat(navigator.appVersion);//全版本号 this.ns = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && //是否 Netscape (agent.indexOf('compatible') == -1))); this.ns2 = (this.ns && (this.major == 3)); //是否 Netscape 2 this.ns3 = (this.ns && (this.major == 3)); //是否 Netscape 3 this.ns4b = (this.ns && (this.minor < 4.04)); //是否 Netscape 4 低版本 this.ns4 = (this.ns && (this.major >= 4)); //是否 Netscape 4 高版本 this.ie = (agent.indexOf("msie") != -1); //是否 IE this.ie3 = (this.ie && (this.major == 2)); //是否 IE 3 this.ie4 = (this.ie && (this.major >= 4)); //是否 IE 4 this.op3 = (agent.indexOf("opera") != -1); //是否 Opera 3 this.win = (agent.indexOf("win")!=-1); //是否 Windows 版本 this.mac = (agent.indexOf("mac")!=-1); //是否 Macintosh 版本 this.unix = (agent.indexOf("x11")!=-1); //是否 Unix 版本 }
var is = new Is();
这个构造函数非常完整的搜集了浏览器的信息。我们看到它为对象定义了很多个属性:major, minor, ns, ie, win, mac 等等。它们的意思见上面的注释。把 is 变量定义为 Is() 对象后,用 if (is.ns) 这种格式就可以很方便的知道浏览器的信息了。我们也可以从这个构造函数中看到,它也可以使用一般的 JavaScript 语句(上例中为 var 语句)。 让我们再来看一个使用参数的构造函数:
function myFriend(theName, gender, theAge, birthOn, theJob) { this.name = theName; this.isMale = (gender.toLowerCase == 'male'); this.age = theAge; this.birthday = new Date(birthOn); this.job = theJob } var Stephen = new myFriend('Stephen', 'Male', 18, 'Dec 22, 1982', 'Student');
从这个构造函数我们不但看到了参数的用法,还看到了不同的属性用不同的数据型是可以的(上例五个属性分别为:字符串,布尔值,数字,日期,字符串),还看到了构造函数里也可以用构造函数来“构造”属性。如果用了足够的“保护措施”来避免无限循环,更可以用构造函数自身来构造自己的属性。
使用 document.cookie 属性。 如果直接使用 document.cookie 属性,或者说,用某种方法,例如给变量赋值,来获得 document.cookie 的值,我们就可以知道在现在的文档中有多少个 Cookies,每个 Cookies 的名字,和它的值。例如,在某文档中添加“document.write(document.cookie)”,结果显示:
name=kevin; email=kevin@kevin.com; lastvisited=index.html
这意味着,文档包含 3 个 Cookies:name, email 和 lastvisited,它们的值分别是 kevin, kevin@kevin.com 和 index.html。可以看到,两个 Cookies 之间是用分号和空格隔开的,于是我们可以用 cookieString.split('; ') 方法得到每个 Cookie 分开的一个数组(先用 var cookieString = document.cookie)。 设定一个 Cookie 的方法是对 document.cookie 赋值。与其它情况下的赋值不同,向 document.cookie 赋值不会删除掉原有的 Cookies,而只会增添 Cookies 或更改原有 Cookie。赋值的格式:
document.cookie = 'cookieName=' + escape('cookieValue') + ';expires=' + expirationDateObj.toGMTString();
是不是看到头晕了呢?以上不是粗体字的地方是要照抄不误的,粗体字是要按实际情况做出改动的。cookieName 表示 Cookie 的名称,cookieValue 表示 Cookie 的值,expirationDateObj 表示储存着失效日期的日期对象名,如果不需要指定失效日期,则不需要第二行。不指定失效日期,则浏览器默认是在关闭浏览器(也就是关闭所有窗口)之后过期。 看到了上面的一些下划线了么?这些是应该注意的地方。 首先 escape() 方法:为什么一定要用?因为 Cookie 的值的要求是“只能用可以用在 URL 编码中的字符”。我们知道“escape()”方法是把字符串按 URL 编码方法来编码的,那我们只需要用一个“escape()”方法来处理输出到 Cookie 的值,用“unescape()”来处理从 Cookie 接收过来的值就万无一失了。而且这两个方法的最常用途就是处理 Cookies。其实设定一个 Cookie 只是“document.cookie = 'cookieName=cookieValue'”这么简单,但是为了避免在 cookieValue 中出现 URL 里不准出现的字符,还是用一个 escape() 好。 然后“expires”前面的分号:注意到就行了。是分号而不是其他。 最后 toGMTString() 方法:设定 Cookie 的时效日期都是用 GMT 格式的时间的,其它格式的时间是没有作用的。 现在我们来实战一下。设定一个“name=rose”的 Cookie,在 3 个月后过期。
var expires = new Date(); expires.setTime(expires.getTime() + 3 * 30 * 24 * 60 * 60 * 1000); /* 三个月 x 一个月当作 30 天 x 一天 24 小时 x 一小时 60 分 x 一分 60 秒 x 一秒 1000 毫秒 */ document.cookie = 'name=rose;expires=' + expires.toGMTString();
为什么没有用 escape() 方法?这是因为我们知道 rose 是一个合法的 URL 编码字符串,也就是说,'rose' == escape('rose')。一般来说,如果设定 Cookie 时不用 escape(),那获取 Cookie 时也不用 unescape()。 再来一次:编写一个函数,作用是查找指定 Cookie 的值。
function getCookie(cookieName) { var cookieString = document.cookie; var start = cookieString.indexOf(cookieName + '='); // 加上等号的原因是避免在某些 Cookie 的值里有 // 与 cookieName 一样的字符串。 if (start == -1) // 找不到 return null; start += cookieName.length + 1; var end = cookieString.indexOf(';', start); if (end == -1) return unescape(cookieString.substring(start)); return unescape(cookieString.substring(start, end)); }
这个函数用到了字符串对象的一些方法,如果你不记得了(你是不是这般没记性啊),请快去查查。这个函数所有的 if 语句都没有带上 else,这是因为如果条件成立,程序运行的都是 return 语句,在函数里碰上 return,就会终止运行,所以不加 else 也没问题。该函数在找到 Cookie 时,就会返回 Cookie 的值,否则返回“null”。 现在我们要删除刚才设定的 name=rose Cookie。
var expires = new Date(); expires.setTime(expires.getTime() - 1); document.cookie = 'name=rose;expires=' + expires.toGMTString();
可以看到,只需要把失效日期改成比现在日期早一点(这里是早 1 毫秒),再用同样的方法设定 Cookie,就可以删掉 Cookie 了。 .
|
一共有 57 条评论