1

获取屏幕缩放比

getDeviceRatio(){ 
    var isAndroid = window.navigator.appVersion.match(/android/gi); 
    var isIPhone = window.navigator.appVersion.match(/iphone/gi); 
    var devicePixelRatio = window.devicePixelRatio; 
    var dpr; 
    if (isIPhone) { 
        // iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
         if (devicePixelRatio >= 3) {
              dpr = 3; 
            } else if (devicePixelRatio >= 2){ 
                dpr = 2; 
            } else { 
                dpr = 1; } 
            } else { // 其他设备下,仍旧使用1倍的方案 
                dpr = 1; 
            } 
            return dpr 
        }

获取URL的参数,返回一个对象

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

设置cookie

 setCookie: function (name, value) {
        var Days = 30;
        var exp = new Date();
        exp.setTime(exp.getTime() + 1000 * 3600 * 24); //过期时间 2分钟
        document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
    },

获取cookie

getCookie: function (Name) {
        var search = Name + "=";
        if (document.cookie.length > 0) {
            var offset = document.cookie.indexOf(search);
            if (offset != -1) {
                offset += search.length;
                var end = document.cookie.indexOf(";", offset);
                if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(offset, end));
            } else
                return "";
        }
    },

删除所有的cookie

 clearCookie(){ 

 var date=new Date();

 date.setTime(date.getTime()-10000);

 var keys=document.cookie.match(/[^ =;]+(?==)/g);

 console.log("需要删除的cookie名字:"+keys);

 if (keys) {

 for (var i = keys.length; i--;)

 document.cookie=keys[i]+"=0; expire="+date.toGMTString()+"; path=/";

 }

 },

HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!