浏览器兼容

最近作浏览器兼容,这大部分是前端做的,我这做后台的有点艰难,谷歌百度的多。在有问题我会持续更新。

1、iframe 标签改成<iframe src=""></iframe> , 不要<iframe src="" /> , 不然像Chrome,Firefox浏览器中多个iframe会只显示一个。

2、获取是那个浏览器。

function myBrowser() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1
            && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.indexOf("Edge") > -1; //判断是否IE的Edge浏览器
    var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1
            && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    var isChrome = userAgent.indexOf("Chrome") > -1
            && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器

    if (isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if (fIEVersion == 7) {
            return "IE7";
        } else if (fIEVersion == 8) {
            return "IE8";
        } else if (fIEVersion == 9) {
            return "IE9";
        } else if (fIEVersion == 10) {
            return "IE10";
        } else if (fIEVersion == 11) {
            return "IE11";
        } else {
            return "0";
        }//IE版本过低
        return "IE";
    }
    if (isOpera) {
        return "Opera";
    }
    if (isEdge) {
        return "Edge";
    }
    if (isFF) {
        return "FF";
    }
    if (isSafari) {
        return "Safari";
    }
    if (isChrome) {
        return "Chrome";
    }
    
}

3、在获取父iframe

parent.window.frames['fraTitle'].showTitle();

4、创建 XMLHttpRequest 对象(在IE5,IE6中有区别)

if (window.XMLHttpRequest)
{
    Request = new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
    Request = new ActiveXObject("Microsoft.XMLHTTP");
}

5、attachEvent为元素添加事件监听器,当触发该事件时调用事件处理函数。

if (window.attachEvent) {  
    window.attachEvent("onload",function(){ o.setup();});
} else if (window.addEventListener) {  
    window.addEventListener("onload",function(){ o.setup();});
}

6、js的function编写格式问题

//
function String.prototype.trim()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}
//改为
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

猜你喜欢

转载自www.cnblogs.com/TimeSay/p/9181747.html