28个常用JavaScript方法集锦(4)


function getOs() {
    if (navigator.userAgent.indexOf("MSIE 8.0") > 0) {
        return "MSIE8";
    }
    else if (navigator.userAgent.indexOf("MSIE 6.0") > 0) {
        return "MSIE6";
    }
    else if (navigator.userAgent.indexOf("MSIE 7.0") > 0) {
        return "MSIE7";
    }
    else if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
        return "Firefox";
    }
    if (navigator.userAgent.indexOf("Chrome") > 0) {
        return "Chrome";
    }
    else {
        return "Other";
    }
}

  

/*  JS判断两个日期大小 适合 2012-09-09 与2012-9-9 两种格式的对比  */

复制代码 代码如下:


//得到日期值并转化成日期格式,replace(/\-/g, "\/")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判断了
        function ValidateDate() {
            var beginDate = $("#t_datestart").val();
            var endDate = $("#t_dateend").val();
            if (beginDate.length > 0 && endDate.length>0) {
                var sDate = new Date(beginDate.replace(/\-/g, "\/"));
                var eDate= new Date(endDate.replace(/\-/g, "\/"));
                if (sDate > eDate) {
                    alert('开始日期要小于结束日期');
                    return false;
                }
            }
        }

  

/* 移除事件 */

复制代码 代码如下:


this.moveBind = function (objId, eventType, callBack) {
    var obj = document.getElementById(objId);
    if (obj.removeEventListener) {
        obj.removeEventListener(eventType, callBack, false);
    }
    else if (window.detachEvent) {
        obj.detachEvent('on' + eventType, callBack);
    }
    else {
        obj['on' + eventType] = null;
    }
}

  

/* 回车提交 */

复制代码 代码如下:


$("id").onkeypress = function (event) {
    event = (event) ? event : ((window.event) ? window.event : "")
    keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
    if (keyCode == 13) {
        $("SubmitLogin").onclick();
    }
}

  

/* JS 执行计时器 */

复制代码 代码如下:


timeStart = new Date().getTime();
timesEnd = new Date().getTime();
document.getElementById("time").innerHTML = timesEnd - timeStart;

 

/* JS 写Cookie */

复制代码 代码如下:


function setCookie(name, value, expires, path, domain) {
    if (!expires) expires = -1;
    if (!path) path = "https://www.jb51.net/";
    var d = "" + name + "=" + value;
    var e;
    if (expires < 0) {
        e = "";
    }
    else if (expires == 0) {
        var f = new Date(1970, 1, 1);
        e = ";expires=" + f.toUTCString();
    }
    else {
        var now = new Date();
        var f = new Date(now.getTime() + expires * 1000);
        e = ";expires=" + f.toUTCString();
    }
    var dm;
    if (!domain) {
        dm = "";
    }
    else {
        dm = ";domain=" + domain;
    }
    document.cookie = name + "=" + value + ";path=" + path + e + dm;
};

  

/* JS 读Cookie  */

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgxwsf.html