当前位置:K88软件开发文章中心电脑基础基础应用13 → 文章内容

javascript教程之让网页记住你的姓名

减小字体 增大字体 作者:华军  来源:华军资讯  发布时间:2019-2-1 22:54:47

 javascript教程之让网页记住你的姓名  实现方法:  一、在页面中添加如下的代码:  《script language=″javascript″》《p》《!--《/p》《p》var username = getcookie(′username′);《/p》《p》if (username == null) {《/p》《p》username = prompt(′please enter your name (otherwise press cancel)′,″″);《/p》《p》if (username == null) {《/p》《p》alert(′its ok if you dont want to tell me your name′);《/p》《p》username = ′websurfer′;《/p》《p》} else {《/p》《p》// don′t set the username cookie.。.. because the user refused to tell you.《/p》《p》// they will be prompted next time.。..《/p》《p》pathname = location.pathname;《/p》《p》mydomain = pathname.substring(0,pathname.lastindexof(′/′)) +′/′;《/p》《p》// set expiry date to 1 year from now.《/p》《p》var largeexpdate = new date ();《/p》《p》largeexpdate.settime(largeexpdate.gettime()+(365*24*3600*1000));《/p》《p》setcookie(′username′,username,largeexpdate,mydomain); } }《/p》《p》function getcookieval (offset) {《/p》《p》var endstr = document.cookie.indexof (″;″, offset);《/p》《p》if (endstr == -1)《/p》《p》endstr = document.cookie.length;《/p》《p》return unescape(document.cookie.substring(offset, endstr)); }《/p》《p》function getcookie (name) {《/p》《p》var arg = name + ″=″;《/p》《p》var alen = arg.length;《/p》《p》var clen = document.cookie.length;《/p》《p》var i = 0;《/p》《p》while (i 《 clen) {《/p》《p》var j = i + alen;《/p》《p》if (document.cookie.substring(i, j)==arg)《/p》《p》return getcookieval (j);《/p》《p》i = document.cookie.indexof(″ ″, i) + 1;《/p》《p》if (i == 0)《/p》《p》break;《/p》《p》} return null; }《/p》《p》function setcookie (name, value) {《/p》《p》var argv = setcookie.arguments;《/p》《p》var argc = setcookie.arguments.length;《/p》《p》var expires = (argc 》 2) ? argv[2] : null;《/p》《p》var path = (argc 》 3) ? argv[3] : null;《/p》《p》var domain = (argc 》 4) ? argv[4] : null;《/p》《p》var secure = (argc 》 5) ? argv[5] : false;《/p》《p》document.cookie = name + ″=″ + escape (value)+((expires == null) ? ″″ : (″; expires=″+expires.togmtstring()))+((path == null) ? ″″ : (″; path=″ + path))+((domain == null) ? ″″ : (″; domain=″ + domain))+((secure == true) ? ″; secure″ : ″″);《/p》《p》}《/p》《p》document.write(′《p》谢谢您的光临, ′+username);《/p》《p》//--》《/script》《/p》  当运行成功之后,会出现一个对话框,要求对方输入姓名,然后就会在网页中出现对方的姓名了。

javascript教程之让网页记住你的姓名