zoukankan      html  css  js  c++  java
  • javascript控制cookies及在跳出本页给出提示,是否放弃操作!!

    1.html
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    </HEAD>
    <script language="JavaScript">
    function Setcookie(name, value) //设置名称为name,值为value的Cookie 
        //var argc = SetCookie.arguments.length; 
        //var argv = SetCookie.arguments; 
        //var path = (argc > 3) ? argv[3] : null; 
        //var domain = (argc > 4) ? argv[4] : null; 
        //var secure = (argc > 5) ? argv[5] : false; 


        document.cookie 
    = name + "=" + value;// + 
        //((path == null) ? "" : ("; path=" + path)) + 
        //((domain == null) ? "" : ("; domain=" + domain)) + 
        //((secure == true) ? "; secure" : ""); 
    }
     

    function Deletecookie(name) //删除名称为name的Cookie 
        var exp = new Date(); 
        exp.setTime (exp.getTime() 
    - 1); 
        
    var cval = GetCookie (name); 
        document.cookie 
    = name + "=" + cval + "; expires=" + exp.toGMTString(); 
    }
     
    function Clearcookie() //清除COOKIE 

        
    var temp=document.cookie.split(";"); 
        
    var loop3; 
        
    var ts; 
        
    for (loop3=0;loop3<temp.length;loop++
            ts
    =temp[loop3].split("=")[0]; 
            
    if (ts.indexOf('mycat')!=-1
            DeleteCookie(ts); 
    //如果ts含“mycat”则执行清除 
        }
     
    }
     

    function getCookieVal(offset) //取得项名称为offset的cookie值 
        var endstr = document.cookie.indexOf (";", offset); 
        
    if (endstr == -1
        endstr 
    = document.cookie.length; 
        
    return unescape(document.cookie.substring(offset, endstr)); 
    }
     

    function GetCookie(name) //取得名称为name的cookie值 
        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 == 0break
        }
     
        
    return null
    }
     
    Setcookie(
    "test","value!!!!!!!!");
    alert(GetCookie(
    "test"));


    function cc() 

    /* check for a cookie */ 
        
    if (document.cookie == "")  
        

            
    /* if a cookie is not found - alert user - 
            change cookieexists field value to false 
    */
     
            alert(
    "COOKIES need to be enabled!");  
            
    /* If the user has Cookies disabled an alert will let him know  
            that cookies need to be enabled to log on.
    */
      
            document.Form1.cookieexists.value 
    ="false"  
        }
     else 
            
    /* this sets the value to true and nothing else will happen, 
            the user will be able to log on
    */
     
            document.Form1.cookieexists.value 
    ="true" 
        }
     
    }
     
    /* Set a cookie to be sure that one exists. 
    Note that this is outside the function
    */
     
    document.cookie 
    = 'killme' + escape('nothing') 

    </script>
    <BODY onbeforeunload="RunOnBeforeUnload()" id="Posts">
    <FORM Name="Form1" id="Form1">
      
    <input type="text" name="cookieexists" value="false">
    </FORM> 
    <href="test.html">中微家校通</a>

    </BODY>
    </HTML>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    g_blnCheckUnload 
    = true;
    function RunOnBeforeUnload() 
    {
        
    if (g_blnCheckUnload)
        
    {
            window.event.returnValue 
    = 你确是要放弃操作吗?'; 
        }

    }

    //-->
    </SCRIPT>

    2.html(简单的操作cookies)
    <html>
    <head>
    <title> 新文档 </title>
    <meta name="Generator" content="EditPlus">
    <meta name="Author" content="FlashSoft">
    <meta name="Keywords" content="">
    <meta name="Description" content="FlashSoft">
    <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
    </head>
    <body>
    <script language="JavaScript" type="Text/JavaScript">
    <!--
    // 实在懒得写了,为了减少字节量
    //
    添加和获得都已经写好了,删除,你只要把时间改为-1就可以了,懒得封装了,实在无意义[代码moz兼容,值得一提,moz跟ie不是一个地方存cookie的]
    function Cookie(){
      
    this.SetValue=function(name,value,hours,path,domain,secure){
        
    var str=new String();
        
    var nextTime=new Date();
        nextTime.setHours(nextTime.getHours()
    +hours);
        str
    =name+"="+escape(value);
        
    if(hours)
          str
    +=";expires="+nextTime.toGMTString();
        
    if(path)
          str
    +=";path="+path;
        
    if(domain)
          str
    +=";domain="+domain;
        
    if(secure)
          str
    +=";secure";
        document.cookie
    =str;
        }

      
    this.GetValue=function(name){
        
    var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
        
    if(tmp=rs)
          
    return unescape(tmp[2]);
        
    return null;
        }

      }

    var _c=new Cookie();
    _c.SetValue(
    "list1","",1);
    _c.SetValue(
    "list2","",1);
    alert(_c.GetValue(
    "list2"))
    document.writeln(document.cookie);
    //-->
    </script>
    </body>
    </html>

  • 相关阅读:
    DevExpress WPF入门指南:加载动画的应用
    DevExpress v17.2—WPF篇(一)
    添加MyEclipse WebSphere Portal Server支持(二)
    DevExpress v17.2—WinForms篇(六)
    MyEclipse 2017 CI 9 发布(附下载)
    DevExpress v17.2新版亮点—WinForms篇(五)
    DevExpress v17.2新版亮点—WinForms篇(四)
    添加MyEclipse WebSphere Portal Server支持(一)
    内嵌iframe页面在IOS下会受内部元素影响自动撑开的问题
    组合 a 标签与 canvas 实现图片资源的安全下载的方法与技巧
  • 原文地址:https://www.cnblogs.com/JimZhang/p/588997.html
Copyright © 2011-2022 走看看