来自https://stackoverflow.com/a/16938481/278722:
function get_browser(){
var N=navigator.appName, ua=navigator.userAgent, tem;
var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
return M[0];
}
您可以使用作为消息 div 的类名返回的浏览器,并且只需更改每个浏览器的样式,如下所示:
<!doctype html>
<html>
<head>
<title>Browser detection</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<style>
#message {
position: fixed;
background: red;
padding: 10px;
font-family: arial;
font-weight: bold;
color: #ffffff;
}
#message.chrome {
width: 100%;
height: 40px;
text-align: right;
}
#message.firefox {
width: 300px;
text-align: left;
left: 50%;
top: 40px;
}
</style>
</head>
<body>
<div id="message">Please click allow geo location</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
var browser = {};
var browserClass;
function get_browser(){
var N=navigator.appName, ua=navigator.userAgent, tem;
var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
return M[0];
}
function init(){
browser = get_browser().toLowerCase();
$("#message").addClass(browser);
}
$(window).on("load",init);
</script>
</body>
</html>