zoukankan      html  css  js  c++  java
  • javascript操作cookie

    在编写前端应用的时候经常需要使用cookie,现在列举一些常用的操作cookie函数。js读取url中的参数

    1) 写入cookie中name,value,expiretime

    function setCookie(c_name,value,expiredays)
    {
        var exdate=new Date()
        exdate.setDate(exdate.getDate()+expiredays)
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" :";expires="+exdate.toGMTString())
    }

    2) 读取cookie的方法

    function getCookie(c_name)
    {
        if (document.cookie.length>0)
          {
              c_start=document.cookie.indexOf(c_name + "=")
              if (c_start!=-1)
              {
                c_start=c_start + c_name.length+1 
                c_end=document.cookie.indexOf(";",c_start)
                if (c_end==-1) 
              c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } } return "" }

    3) 删除cookie的函数

    function clearCookie(c_name) {  
        setCookie(name, "", -1);  
    }

    2 使用js操作url中的内容

      1) 使用js获取url的函数location.href,此时获取了全部的url内容

      2) 使用js获取url中的参数,url中的参数是?开始,使用location.search,即可获取url中的参数,包含'?'

      3)  读取url中的参数,并保存在对象中,使用对象获取请求参数

      获取url中参数并保存到对象中返回,函数如下:

    function GetRequest() { 
      var url = location.search; //获取url中"?"符后的字串 
      var theRequest = new Object(); 
      if (url.indexOf("?") != -1) { 
        var str = url.substr(1); 
        strs = str.split("&"); 
        for(var i = 0; i < strs.length; i ++) { 
          theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); 
        } 
      } 
      return theRequest; 
    } 

      调用对象方法来获取请求参数

      

    var Request = new Object(); 
    Request = GetRequest(); 
    var 参数1,参数2,参数3,参数N; 
    参数1 = Request['参数1']; 
    参数2 = Request['参数2']; 
    参数3 = Request['参数3']; 
    参数N = Request['参数N']; 
  • 相关阅读:
    码农提高工作效率 (转)
    Python快速教程 尾声
    C#基础——谈谈.NET异步编程的演变史
    [C#]動態叫用Web Service
    零极限 核心中的核心和详解
    项目经理应该把30%的时间用在编程上
    高效能程序员的七个习惯
    我们如何进行代码审查
    工作经常使用的SQL整理,实战篇(二)
    C# Socket网络编程精华篇 (转)
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/5951920.html
Copyright © 2011-2022 走看看