检测不支持的浏览器版本并显示带有消息的特定 div [英] Detect unsupported browser version and show specific div with message

查看:27
本文介绍了检测不支持的浏览器版本并显示带有消息的特定 div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有一个聊天机器人,它需要最新版本的浏览器才能完美运行,所以我需要向用户显示一条消息,说请将您的浏览器更新到最新版本".我不想使用第三方插件.如果用户使用此 js 代码使用不受支持的浏览器版本,我如何显示 div

I have a chat bot in my website it need latest version of browsers to work perfectly so I need to show a message to user saying "Please update your browser to latest version". I don't want to use third party plugin. How can i show a div if the user is using unsupported version of browsers using this js code

HTML

<div id="printVer"></div>

JS

navigator.sayswho= (function(){
    var ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();
document.getElementById('printVer').innerHTML=navigator.sayswho

推荐答案

navigator.sayswho = ( function () {
    var ua = navigator.userAgent, tem,
        M = ua.match( /(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i ) || [];
    if ( /trident/i.test( M[1] ) ) {
        tem = /\brv[ :]+(\d+)/g.exec( ua ) || [];
        return 'IE ' + ( tem[1] || '' );
    }
    if ( M[1] === 'Chrome' ) {
        tem = ua.match( /\b(OPR|Edge)\/(\d+)/ );
        if ( tem != null ) return tem.slice( 1 ).join( ' ' ).replace( 'OPR', 'Opera' );
    }
    M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
    if ( ( tem = ua.match( /version\/(\d+)/i ) ) != null ) M.splice( 1, 1, tem[1] );
    return M.join( ' ' );
} )();
//document.getElementById('printVer').innerHTML=navigator.sayswho
var str = navigator.sayswho;
var browser = str.substring( 0, str.indexOf( " " ) );
var version = str.substring( str.indexOf( " " ) );
version = version.trim();
version = parseInt( version );
console.log( browser );
console.log( version );

if ( ( browser == "Chrome" && version < 70 ) || ( browser == "Firefox" && version < 53 ) || ( browser == "Safari" && version < 5 ) || ( browser == "IE" && version < 11 ) || ( browser == "Opera" && version < 52 ) ) {
    $( '#printVer' ).show();
}
else {
    $( '#printVer' ).hide();
}

这篇关于检测不支持的浏览器版本并显示带有消息的特定 div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆