实现的具体事件是这样的:
例如,当某用户第一次进入页面时,你可以使用消息框来询问用户的姓名。姓名会保存在 cookie 中。当用户再次进入这个页面时,你可以使用另一个消息框来和这个用户打招呼:"Welcome!"。
初学Javascript,大家多多关照

解决方案 »

  1.   

    只需要ONLOAD事件吧。
    ONLOAD写一个JS函数,判断指定的COOKIE是否为空。
    如果为空,设定指定的COOKIE为用户姓名。
    不为空,则弹出这个姓名。
    JS获取和设置COOKIE的方法
      

  2.   


    //设置cookie
    function setCookie(name,value) {
        var Days = 1;
        var exp  = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
    }
    //获取cookie
    function getCookie(name) {
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) return unescape(arr[2]);
        else return "";
    }setCookie("userName",value);
    onload的时候判断下getCookie("userName")==null就可以了 .
      

  3.   

    你是不是要这种 判断首次加载的功能
    首次登录 需要输入 客人名称,确定并 记录COOKIE 跳转
    显示 welcome 客人名字
    COOKIE 的设置应该都知道吧<script> String.prototype.Trim = function(){ return this.replace(/^\s+/g,"").replace(/\s+$/g,""); } 
    function JSCookie() 
    {
    this.GetCookie = function(key) { 
    var cookie = document.cookie;
    var cookieArray = cookie.split(';'); 
    var getvalue = ""; 
    for(var i = 0;i<cookieArray.length;i++) 

    if(cookieArray[i].Trim().substr(0,key.length) == key)
    { getvalue = cookieArray[i].Trim().substr(key.length + 1); break;

    } return getvalue; };
    this.GetChild = function(cookiekey,childkey) 

    var child = this.GetCookie(cookiekey); 
    var childs = child.split('&');
    var getvalue = ""; 
    for(var i = 0;i < childs.length;i++)
    {
    if(childs[i].Trim().substr(0,childkey.length) == childkey) 
    {
    getvalue = childs[i].Trim().substr(childkey.length + 1); break;


    return getvalue; }; 
    this.SetCookie = function(key,value,expire,domain,path)
    { var cookie = ""; if(key != null && value != null) cookie += key + "=" + value + ";";
    if(expire != null) cookie += "expires=" + expire.toGMTString() + ";";
    if(domain != null) cookie += "domain=" + domain + ";";
    if(path != null) cookie += "path=" + path + ";"; document.cookie = cookie; };
    this.Expire = function(key) {
    expire_time = new Date(); 
    expire_time.setFullYear(expire_time.getFullYear() - 1);
    var cookie = " " + key + "=e;expires=" + expire_time + ";";
    document.cookie = cookie;
    }

    window.onload=function()
    {
    var cookie = new JSCookie(); 
    var n = cookie.GetCookie("first");
    if(n=="")

    this.username.style.display='';
    }
    else{
    this.username.style.display='none';
    document.write("welcome "+n);
    }

    function name_submit()
    {
    var cookie = new JSCookie(); 
    var expire_time = new Date();
    expire_time.setFullYear(expire_time.getFullYear() + 1);
    cookie.SetCookie("first",this.guest_name.value,expire_time); 
    window.location=location.href;
    }</script> <div name='username' id="username">
    guest ! Please  enter your  name :
        <input type="text" name="guest_name" id="guest_name"/> <input type="button" onclick="name_submit()" value="-OK-"/>
    </div>
      

  4.   

    保存用户状态,cookie最好在服务器端写入,如果要取出来的话,可以在服务器端,也可以在浏览器
    浏览器取cookie的函数
    function getCookie(key)
    {
        var cookie = document.cookie;
        if(cookie == undefined || cookie == "") return undefined
        var regex = new RegExp("(?:"+ key +"=)([^&]+)","gi");
        if(regex.test(cookie)) return (RegExp.$1)
        return undefined;
    }var str = getCookie("cookie的名字")
    这样就行了