Js方法集

收集一些常用方法,思之补之踩过的坑;

判定浏览器是否为移动端,然后to do something:

$(function isMobileBrowser()
{
    var sUserAgent = navigator.userAgent.toLowerCase();    
    var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";    
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";    
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";    
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";    
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";    
    var bIsAndroid = sUserAgent.match(/android/i) == "android";    
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";    
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";    
    if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM ){    
        return true;
    }else 
        return false;
})()

曾经的错误…,js可以获取上一个访问的页面,可用于多个场景,并且刷新页面此值不变:

document.referrer

js中计算一些带小数的数字会得出小数点非常长的现象例如2.01-1.02=0.9899999999999998,因为js中是将数字转成二进制进行计算然后再转成数字:

var ComputAmount = function(arg1, arg2, computType) {
    var r1, r2, m;
    try {
        r1 = arg1.toString().split(".")[1].length
    } catch(e) {
        r1 = 0
    }
    try {
        r2 = arg2.toString().split(".")[1].length
    } catch(e) {
        r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    if(computType == 1) {
        return((arg1 * m + arg2 * m) / m).toFixed(2);
    } else {
        return((arg1 * m - arg2 * m) / m).toFixed(2);
    }
}

三个参数,第一个数字,第二个数字,第三个是加减的参数,改动可以用于乘除;

判断url是否存在某search参数,稍改动可获取某search参数的值:

function giveQuery(p) {
    var httppath = "www.baidu.com?a=b&c=d",
        httppath = httppath.substring(13); //?a=b&c=d  此处可优化
    //var httppath=location.search; //?a=b&c=d
    search = httppath.length > 0 ? httppath.substring(1) : "", //a=b&c=d
        items = search.length > 0 ? search.split("&") : [], //["a=b", "c=d"]
        item = null,
        t = false,
        len = items.length; //2
 
    for(i = 0; i < len; i++) {
        item = items[i].split("=");
        if(item[0] == p) {
            console.log(item[1]);
            t = true;
        }
    }
    if(!t) {
        console.log("search is null")
    }
}

读取Cookie值:

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

浏览器居中显示提示消息,移动端应用的多些:

var alertMsg = function(text) {
    //拼接div 或者display div 
    //class=alertBg 设置透明蒙版等
    $("body").append('<div class="alertBg" id="alertMsg">' + text + '</div>');
 
    var $height = $("#alertMsg").outerHeight() / 2,
        $width = $("#alertMsg").outerWidth() / 2;
 
    $("#alertMsg").css({
        marginLeft: -$width,
        marginTop: -$height
    });
 
    $("#alertMsg").show().fadeOut(3000, function() {
        $(this).remove();
    });
}

旧版浏览器(IE678)提示:

var isIE7older = document.all && !document.querySelector,
isIE8older = document.all && document.querySelector && !document.addEventListener;
if(isIE7older || isIE8older) {
    doSomeThing()
}

原版 2017-03-07发布于xuechenlei.com

原文地址:https://www.cnblogs.com/xuechenlei/p/10949742.html