js常见方法封装

1、判断是否是微信浏览器

function isWechat() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

2、判断是否是PC/WAP端

function isPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}

3、获取URL参数

function getUrlParam(name, url) {
    if (!url) url = location.href;
    if (url.indexOf('?') == -1) return '';
    try {
        var re = new RegExp("" + name + "=([^&?]*)", "ig");
        return ((url.match(re)) ? (decodeURIComponent(url.match(re)[0].substr(name.length + 1))) : '');
    } catch (_e) {
        return '';
    }
}

4、获取cookie

function getCookie(name, defaultVal = null) {
    var arr,
    reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        return defaultVal;
    }
}

5、设置cookie

function setCookie(name, value, seconds) {
    var expires = new Date();
    expires.setTime(expires.getTime() + parseInt(seconds) * 1000);
    var domain = document.domain;
    var domainArr = domain.split('.');
    if (domainArr.length >= 3) {
        domainArr.shift();
        domain = domainArr.join('.');
    }    
    document.cookie = escape(name) + '=' + escape(value) + (seconds ? ('; expires=' + expires.toGMTString()) : "") + '; path=/; domain=.' + domain + ';';
}

6、删除cookie

// this指上面的方法
function delCookie(name) {
    this.setCookie(name, '', -1);
}

7、保存网页到桌面

function toDesktop(sUrl,sName){
    try {
        var WshShell = new ActiveXObject("WScript.Shell");
        var oUrlLink = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop")+ "\\" + sName + ".url");
        oUrlLink.TargetPath = sUrl;
        oUrlLink.Save();
    }catch(e){
        alert("当前IE安全级别不允许操作!");
    }
}

8、添加网页到收藏

function AddFavorite(title, url){
    try {
        window.external.addFavorite(url, title);
    }catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        }catch (e) {
            alert("抱歉,您所使用的浏览器无法完成此操作。\n\n加入收藏失败,请使用Ctrl+D进行添加");
        }
    }
}

9、判断是否为空

function isEmpty(val){
    if(typeof val == "undefined" || val == null || val == ""){
        return true;
    } else {
        return false;
    }
}

10、值是否在数组内

function inArray(search, array) {
    for (var i in array) {
        if (array[i] == search) {
            return true;
        }
    }
    return false;
}

11、数组去重

function arrayUnique(arr) {
    return [...new Set(arr)];
}

12、设置Localstorage

function setLocalstorage(key, value) {
    if(!window.Storage) {
        console.error("该浏览器不支持Localstorage属性!!");
        return false;
    } else {
        localStorage.setItem(key, value);
    }
}

13、获取Localstorage

function getLocalstorage(key) {
    var value = localStorage.getItem(key);
    if (value) {
        try {
            var value_json = JSON.parse(value);
            if (typeof value_json === 'object') {
                return value_json;
            } else if (typeof value_json === 'number') {
                return value_json;
            }
        } catch(e) {
            return value;
        }
    } else {
        return false;
    }
}

14、移除单个Localstorage

function removeLocalstorage(key) {
    localStorage.removeItem(key);
}

15、清除整个Localstorage

function clearLocalstorage() {
    localStorage.clear();
}

 

你可能感兴趣的:(高级前端,js,经验分享)