网学网Javascript编辑为广大网友搜集整理了:增添靓丽风景 让网页记住你的姓名绩等信息,祝愿广大网友取得需要的信息,参考学习。
大家有没有这样的想法呢?就是当我们在浏览某个站点或者是页面时,可以把浏览者的姓名给记录下来,这样可以为你的主页增添了一道靓丽的风景线。
实现方法:
一、在页面中添加如下的代码:
<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>
<!--
var username = getcookie(′username′);
if (username == null) {
username = prompt(′please enter your name (otherwise press cancel)′,″″);
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>