utils.min.js 4.26 KB
var Utils={checkDevice:function(){var ua=navigator.userAgent;var device="ios";if(ua.match(/android/i)){device="android"}else{if(ua.match(/iphone|ipad|ipod/)){device="ios"}else{device="ios"}}return device},setCookie:function(name,value,expires){var date;if(typeof value!=="undefined"){if(value===null){value="";expires=-1}if(expires&&(typeof expires==="number"||expires.toUTCString)){if(typeof expires==="number"){date=new Date();date.setTime(date.getTime()+(expires*24*60*60*1000))}else{date=expires}expires="; expires="+date.toUTCString()}else{date=new Date();expires="; expires="+date.setTime(date.getTime()+(24*60*60*1000))}document.cookie=[name,"=",encodeURIComponent(value),expires].join("")}},getCookie:function(name){var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");if(arr=document.cookie.match(reg)){return decodeURIComponent(arr[2])}else{return null}},delCookie:function(name){var exp=new Date();exp.setTime(exp.getTime()-1);var cval=this.getCookie(name);if(cval!==null&&cval!==undefined){document.cookie=name+"="+cval+";expires="+exp.toGMTString()}},setlocalData:function(name,value){if(this.checkDevice()==="ios"){localStorage.setItem(name,value)}if(this.checkDevice()==="android"){this.setCookie(name,value)}},getlocalData:function(name){var value="";if(this.checkDevice()==="ios"){value=localStorage.getItem(name)}if(this.checkDevice()==="android"){value=this.getCookie(name)}return value},dellocalData:function(name){if(this.checkDevice()==="ios"){localStorage.removeItem(name)}if(this.checkDevice()==="android"){this.delCookie(name)}},manageQueryString:function(){var loc=document.location.href;var variables="";var variableArr=[];var finalArr=[];if(loc.indexOf("?")>0){variables=loc.split("?")[1]}if(variables.length>0){variableArr=variables.split("&")}for(var i=0;i<variableArr.length;i++){var obj={};obj.name=variableArr[i].split("=")[0];obj.value=variableArr[i].split("=")[1];finalArr.push(obj)}return finalArr},getQueryObj:function(){var query_arr=this.manageQueryString();var query_obj={};for(var i=0;i<query_arr.length;i++){query_obj[query_arr[i].name]=query_arr[i].value}return query_obj},getQueryStringObject:function(str){var vars={},hashes,hash,i;str=str||"";if(str){hashes=str.split("&");for(i=0;i<hashes.length;i++){hash=hashes[i].split("=");vars[hash[0]]=hash[1]}}return vars},getTransform:function(transform){var tArr=transform.split(" ");var transObj={};if(tArr.length>0){for(var i=0;i<tArr.length;i++){var obj={};var itemArr=tArr[i].split("(");var trans_name=itemArr[0];var trans_value=itemArr[1].replace(")","");if(trans_name=="translateX"){transObj.translateX=trans_value}else{if(trans_name=="translateY"){transObj.translateY=trans_value}else{if(trans_name=="rotate"){transObj.rotate=trans_value}else{if(trans_name=="rotateY"){transObj.rotateY=trans_value}}}}}}return transObj},getStrLength:function(str){var realLength=0,len=str.length,charCode=-1;for(var i=0;i<len;i++){charCode=str.charCodeAt(i);if(charCode>=0&&charCode<=128){realLength+=1}else{realLength+=2}}return realLength},cutStr:function(str,len){var str_length=0;var str_len=0;str_cut="";str_len=str.length;for(var i=0;i<str_len;i++){a=str.charAt(i);str_length++;if(encodeURIComponent(a).length>4){str_length++}str_cut=str_cut.concat(a);if(str_length>=len){return str_cut}}if(str_length<len){return str}},getVideo:function(id,src){var video_html='<video id="'+id+'" style="width:1px;height:1px;" preload="auto" webkit-playsinline playsinline controls="controls">'+'<source src="'+src+'" type="video/mp4">'+"</video>";return video_html},initRequestAnimationFrames:function(){var prefix;for(var i=0;i<prefixes.length;i++){if(requestAnimationFrame&&cancelAnimationFrame){break}prefix=prefixes[i];requestAnimationFrame=requestAnimationFrame||window[prefix+"RequestAnimationFrame"];cancelAnimationFrame=cancelAnimationFrame||window[prefix+"CancelAnimationFrame"]||window[prefix+"CancelRequestAnimationFrame"]}if(!requestAnimationFrame||!cancelAnimationFrame){requestAnimationFrame=function(callback){var currTime=new Date().getTime();var timeToCall=Math.max(0,16-(currTime-lastTime));var id=window.setTimeout(function(){callback(currTime+timeToCall)},timeToCall);lastTime=currTime+timeToCall;return id};cancelAnimationFrame=function(id){window.clearTimeout(id)}}window.requestAnimationFrame=requestAnimationFrame;window.cancelAnimationFrame=cancelAnimationFrame}};