Javascript 简明教程
JavaScript - Browsers Compatibility
为了以预期的方式处理每个浏览器,重要的是了解不同浏览器之间的差异。因此,了解您的网页正在哪个浏览器中运行非常重要。
用内置 navigator 对象获取您的网页当前正在运行的浏览器信息。
Browser Detection
有一个简单的 JavaScript,它可以用来找出浏览器的名称,然后可以向用户提供相应 HTML 页面。
<html>
<head>
<title>Browser Detection Example</title>
</head>
<body>
<script type = "text/javascript">
var userAgent = navigator.userAgent;
var opera = (userAgent.indexOf('Opera') != -1);
var ie = (userAgent.indexOf('MSIE') != -1);
var gecko = (userAgent.indexOf('Gecko') != -1);
var netscape = (userAgent.indexOf('Mozilla') != -1);
var version = navigator.appVersion;
if (opera) {
document.write("Opera based browser");
// Keep your opera specific URL here.
} else if (gecko) {
document.write("Mozilla based browser");
// Keep your gecko specific URL here.
} else if (ie) {
document.write("IE based browser");
// Keep your IE specific URL here.
} else if (netscape) {
document.write("Netscape based browser");
// Keep your Netscape specific URL here.
} else {
document.write("Unknown browser");
}
// You can include version to along with any above condition.
document.write("<br /> Browser version info : " + version );
</script>
</body>
</html>