Home > Web Front-end > JS Tutorial > Explain some common JS method encapsulation

Explain some common JS method encapsulation

coldplay.xixi
Release: 2021-02-08 17:46:15
forward
2442 people have browsed it

Explain some common JS method encapsulation

免费学习推荐:js视频教程

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

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

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;
}
Copy after login

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 '';
    }
}
Copy after login

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;
    }
}
Copy after login

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 + ';';
}
Copy after login

6、删除cookie

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

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安全级别不允许操作!");
    }
}
Copy after login

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进行添加");
        }
    }
}
Copy after login

9、判断是否为空

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

10、值是否在数组内

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

11、数组去重

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

12、设置Localstorage

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

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;
    }
}
Copy after login

14、移除单个Localstorage

function removeLocalstorage(key) {
    localStorage.removeItem(key);
}
Copy after login

15、清除整个Localstorage

function clearLocalstorage() {
    localStorage.clear();
}
Copy after login

相关免费学习推荐:javascript(视频)

The above is the detailed content of Explain some common JS method encapsulation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template