当前位置:首页 > 网页设计 >Javascript >

让网页记住你的姓名 增添靓丽风景

发布时间:2015-11-21 10:52:37 作者:佚名 阅读:(194)

大家有没有这样的想法呢?就是当我们在浏览某个站点或者是页面时,可以把浏览者的姓名给记录下来,这样可以为你的主页增添了一道靓丽的风景线。
  实现方法:
  一、在页面中添加如下的代码:
<script language=javascript″>
<!--
var username = getcookie(′username′);
if (username == null) {
username = prompt(′please enter your name (otherwise press cancel)′,″″);
if (username == null) {
alert(′its ok if you dont want to tell me your name′);
username = ′websurfer′;
} else {
// don′t set the username cookie.... because the user refused to tell you.
// they will be prompted next time....
pathname = location.pathname;
mydomain = pathname.substring(0,pathname.lastindexof(′/′)) ′/′;
// set expiry date to 1 year from now.
var largeexpdate = new date ();
largeexpdate.settime(largeexpdate.gettime()(3652436001000));
setcookie(′username′,username,largeexpdate,mydomain); } }
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″ : ″″);
}
document.write(′<p>谢谢光临, ′username);
//--></script>
  当运行成功之后,会出现一个对话框,要求对方输入姓名,然后就会在网页中出现对方的姓名了。
 

 

欢迎分享转载→ 让网页记住你的姓名 增添靓丽风景

© 2015-2019 - 吾爱编程网 版权所有 苏ICP备18033726号-1关于我们 - 网站声明 - 联系我们