二十七个常用JavaScript方法集锦,js输出列表达成

作者: 人才发展  发布:2019-09-18

数据库用UTF-8,现存系统用gb2312,

有时应用的 JS 方法,昨天记下,以便以后查询

用<script>echo_script(url);</script>

/* 手提式有线电话机类型剖断 */

复制代码 代码如下:

复制代码 代码如下:

<script>
function echo_script(url){

var BrowserInfo = {
    userAgent: navigator.userAgent.toLowerCase()
    isAndroid: Boolean(navigator.userAgent.match(/android/ig)),
    isIphone: Boolean(navigator.userAgent.match(/iphone|ipod/ig)),
    isIpad: Boolean(navigator.userAgent.match(/ipad/ig)),
    isWeixin: Boolean(navigator.userAgent.match(/MicroMessenger/ig)),
}

var USERAGENT = navigator.userAgent.toLowerCase();
var bname = '';
var b = ['ie','msie','firefox','chrome','opera','safari','maxthon','mozilla','webkit'];
for(i in b){
if(USERAGENT.indexOf(b[i]) > -1){
bname = b[i];
break;
}
}
if(!bname)bname='other';
var ie8 = USERAGENT.indexOf('msie 8') > -1 ? 'ie8' : false;
var ie7 = USERAGENT.indexOf('msie 7') > -1 ? 'ie7' : false;

/* 重返字符串长度,男士计数为2  */

if(bname == 'firefox' || ie8 )
{
var enc = "no";
}
if(ie8){
bname = 'ie8';
}else if(ie7){
bname = 'ie7';
}

复制代码 代码如下:

var tmp_ie8 = ie8 ? 'ie8' : 'no_ie8';

function strLength(str) {
    var a = 0;
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) > 255)
            a += 2;//依据预期计数扩展2
        else
            a++;
    }
    return a;
}

document.write("<script src='"+url+"&encode="+enc+"&browser="+tmp_ie8+"&browserv="+bname+"'></script>");
}
</script>

得到url中的参数

哎呀,这种方法,导致在分歧的浏览器下乱码不断,害苦作者了。
有更简短的主意

复制代码 代码如下:

<script type="text/javascript" charset="utf-8" src="xx.com/xx.php"></script>
乱码从此不再出现!

function GetQueryStringRegExp(name,url) {
    var reg = new RegExp("(^|\?|&)" + name + "=([^&]*)(\s|&|$)", "i");
    if (reg.test(url)) return decodeURIComponent(RegExp.$2.replace(/+/g, " ")); return "";
}

  

/* js 绑定事件 适用于任何浏览器的因素绑定  */

复制代码 代码如下:

function eventBind(obj, eventType, callBack) {
        if (obj.addEventListener) {
            obj.addEventListener(eventType, callBack, false);
        }
        else if (window.attachEvent) {
            obj.attachEvent('on' + eventType, callBack);
        }
        else {
            obj['on' + eventType] = callBack;
        }
    };
eventBind(document, 'click', bodyClick);

  

/* 获得当前浏览器JS的版本 */

复制代码 代码如下:

function getjsversion(){
    var n = navigator;
    var u = n.userAgent;
    var apn = n.appName;
    var v = n.appVersion;
    var ie = v.indexOf('MSIE ');
    if (ie > 0){
        apv = parseInt(i = v.substring(ie + 5));
        if (apv > 3) {
            apv = parseFloat(i);
        }
    } else {
        apv = parseFloat(v);
    }
    var isie = (apn == 'Microsoft Internet Explorer');
    var ismac = (u.indexOf('Mac') >= 0);
    var javascriptVersion = "1.0";
    if (String && String.prototype) {
        javascriptVersion = '1.1';
        if (javascriptVersion.match) {
            javascriptVersion = '1.2';
            var tm = new Date;
            if (tm.setUTCDate) {
                javascriptVersion = '1.3';
                if (isie && ismac && apv >= 5) javascriptVersion = '1.4';
                var pn = 0;
                if (pn.toPrecision) {
                    javascriptVersion = '1.5';
                    a = new Array;
                    if (a.forEach) {
                        javascriptVersion = '1.6';
                        i = 0;
                        o = new Object;
                        tcf = new Function('o', 'var e,i=0;try{i=new Iterator(o)}catch(e){}return i');
                        i = tcf(o);
                        if (i && i.next) {
                            javascriptVersion = '1.7';
                        }
                    }
                }
            }
        }
    }
    return javascriptVersion;
}

/* 获取当前点击事件的Object对象 */

复制代码 代码如下:

function getEvent() {
    if (document.all) {
        return window.event; //如果是ie
    }
    func = getEvent.caller;
    while (func != null) {
        var arg0 = func.arguments[0];
        if (arg0) {
            if ((arg0.constructor == Event || arg0.constructor == MouseEvent)
|| (typeof (arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) {
                return arg0;
            }
        }
        func = func.caller;
    }
    return null;
};

  

/* 字符串截取方法  */

复制代码 代码如下:

getCharactersLen: function (charStr, cutCount) {
        if (charStr == null || charStr == '') return '';
        var totalCount = 0;
        var newStr = '';
        for (var i = 0; i < charStr.length; i++) {
            var c = charStr.charCodeAt(i);
            if (c < 255 && c > 0) {
                totalCount++;
            } else {
                totalCount += 2;
            }
            if (totalCount >= cutCount) {
                newStr += charStr.charAt(i);
                break;
            }
            else {
                newStr += charStr.charAt(i);
            }
        }
        return newStr;
    }

  

/*  JS 弹出新窗口全屏  */

复制代码 代码如下:

var tmp = window.open("about:blank", "", "fullscreen=1")
                            tmp.moveTo(0, 0);
                            tmp.resizeTo(screen.width + 20, screen.height);
                            tmp.focus();
                            tmp.location.href = '' + msgResult.message[0] + '.html';
 
var config_ = "left=0,top=0,width=" + (window.screen.Width) + ",height=" + (window.screen.Height);
                            window.open('' + msgResult.message[0] + '.html', "winHanle", config_);
//模拟form提交张开新页面
var f = document.createElement("form");
                            f.setAttribute('action', '' + msgResult.message[0] + '.html');
                            f.target = '_blank';
                            document.body.appendChild(f);
                            f.submit();

/*   全选/全不选  */

复制代码 代码如下:

function selectAll(objSelect) {
            if (objSelect.checked == true) {
                $("input[name='chkId']").attr("checked", true);
                $("input[name='chkAll']").attr("checked", true);
            }
            else if (objSelect.checked == false) {
                $("input[name='chkId']").attr("checked", false);
                $("input[name='chkAll']").attr("checked", false);
            }
        }

  

/* js 决断浏览器  */

复制代码 代码如下:

看清是或不是是 IE 浏览器
    if (document.all){
        alert(”IE浏览器”);
    }else{
        alert(”非IE浏览器”);
    }
    if (!!window.ActiveXObject){
        alert(”IE浏览器”);
    }else{
        alert(”非IE浏览器”);
    }
判断是IE几
var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
var isIE8=isIE&&!!document.documentMode;
var isIE7=isIE&&!isIE6&&!isIE8;
if (isIE){
    if (isIE6){
        alert(”ie6″);
    }else if (isIE8){
        alert(”ie8″);
    }else if (isIE7){
        alert(”ie7″);
    }
}

  

/* 判定浏览器 */

复制代码 代码如下:

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判别四个日子大小 适合 二零一一-09-09 与贰零壹壹-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();
    }
}

  

本文由bet9九州发布于人才发展,转载请注明出处:二十七个常用JavaScript方法集锦,js输出列表达成

关键词:

上一篇:eval解析JSON中的注意点介绍
下一篇:没有了