实时编辑dom的代码,包含新的判断浏览器核心及版本的方法,留存参考

//"mozilla/5.0 (windows nt 6.3; wow64; trident/7.0; .net4.0e; .net4.0c; infopath.3; .net clr 3.5.30729; .net clr 2.0.50727; .net clr 3.0.30729; rv:11.0) like gecko"
//"mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, like gecko) chrome/46.0.2490.80 safari/537.36"
// mozilla/5.0 (windows nt 6.3; wow64; rv:43.0) gecko/20100101 firefox/43.0
//"mozilla/5.0 (windows nt 6.3; wow64; trident/7.0; .net4.0e; .net4.0c; infopath.3; .net clr 3.5.30729; .net clr 2.0.50727; .net clr 3.0.30729; qqbrowser/8.0.3328.400; rv:11.0) like gecko" 

function browserInfo() {
    var agent = navigator.userAgent.toLowerCase(),
        regStr_ie = /msie [\d.]+;/gi,
        regStr_ierv = /rv:[\d.]+/gi,
        regStr_ff = /firefox\/[\d.]+/gi,
        regStr_chrome = /chrome\/[\d.]+/gi,
        regStr_saf = /safari\/[\d.]+/gi,
        browser = {};
    if(agent.indexOf("msie") > 0) {
        browser.info = agent.match(regStr_ie) ;
    }
    if(agent.indexOf("rv:") > 0) {
        browser.info = agent.match(regStr_ierv);
    }
    if(agent.indexOf("firefox") > 0) {
        browser.info = agent.match(regStr_ff);
    }
    if(agent.indexOf("chrome") > 0) {
        browser.info = agent.match(regStr_chrome);
    }
    if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) {
        browser.info = agent.match(regStr_saf);
    }
    browser.version = (browser.info + "").replace(/[^0-9.]/ig, "");
    return browser;
}
var insertHtml = function (html) {
    var selection = window.getSelection ? window.getSelection() : document.selection,
        range = selection.createRange ? selection.createRange() : selection.getRangeAt(0);
    var browser = browserInfo();
    if (browser.info.indexOf('msie') > 0 && browser.version < 9) {
        range.pasteHTML(html);
    } else {
        //var node = document.createElement('span');
        //node.innerHTML = html;
        //range.insertNode(node);
        range.insertNode($(html)[0]);
        selection.addRange(range);
    }
}
var insertImg = function (img) {
    this.insertHtml("<img src='" + img + "'/>");
}

posted on 2016-01-04 10:58  丰云  阅读(109)  评论(0编辑  收藏  举报

导航