大家有没有这样的想法呢?就是当我们在浏览某个站点或者是页面时,可以把浏览者的姓名给记录下来,这样可以为你的主页增添了一道靓丽的风景线。
实现方法:
一、在页面中添加如下的代码:
[code]<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()+(365*24*3600*1000));
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>
[/code][button value="复制代码"]当运行成功之后,会出现一个对话框,要求对方输入姓名,然后就会在网页中出现对方的姓名了。