我用于返回html文档使用的当前浏览器版本的代码返回NaN。我已经完成了代码,对此感觉还不错,但是navigator.useragent没有返回正确的输出。
/// javascript
function getBrowserName() {
var lsBrowser = navigator.userAgent;
if (lsBrowser.indexOf("MSIE") >= 0) {
return "MSIE";
} else if (lsBrowser.indexOf("Firefox") >= 0) {
return "Firefox";
} else if (lsBrowser.indexOf("Chrome") >= 0) {
return "Chrome";
} else if (lsBrowser.indexOf("Safari") >= 0) {
return "Safari";
} else if (lsBrowser.indexOf("Opera") >= 0) {
return "Opera";
} else {
return "UNKNOWN";
}
}
function getBrowserVersion() {
var ua = navigator.userAgent;
var browser = getBrowserName();
var findIndex = ua.indexOf(browser) + browser.length + 1;
var browserVersion = parseFloat(ua.substring(findIndex, findIndex +3));
return browserVersion;
}
var browserName = getBrowserName();
var browserVersion = getBrowserVersion();
var windowHeight = window.screen.availHeight;
var windowWidth = window.screen.availWidth;
document.write("You are using " + browserVersion + " version of the "
+ browserName + " browser to view this page in a browser whose inner" +
" window that is" + windowWidth + " pixels wide and " +
windowHeight + " pixels tall.");
/// html doc
<!DOCTYPE html>
<html lan= "en">
<head>
<title> Chapter 8 Browser Sniffing</title>
</head>
<body>
<script src = "browserSniffing.js"></script>
</body>
</html>
我希望我的代码能填补以下空白: 您正在使用____________浏览器的_________版本在其内部窗口宽_____像素和_____像素的浏览器中查看此页面。 它充满了他们。但是getBrowserVersion函数返回NaN。也许有人会就如何稍微调整此代码以使其正常工作提供一些建议。
答案 0 :(得分:1)
尝试一下:
/// javascript
function getBrowserName() {
var lsBrowser = navigator.userAgent;
debugger
if (lsBrowser.indexOf("MSIE") >= 0) {
return "MSIE";
} else if (lsBrowser.indexOf("Firefox") >= 0) {
return "Firefox";
} else if (lsBrowser.indexOf("Chrome") >= 0) {
return "Chrome";
} else if (lsBrowser.indexOf("Safari") >= 0) {
return "Safari";
} else if (lsBrowser.indexOf("Opera") >= 0) {
return "Opera";
} else {
return "UNKNOWN";
}
}
function getBrowserVersion() {
debugger
var ua = navigator.userAgent;
var browser = getBrowserName();
var findIndex = ua.indexOf(browser) + browser.length + 1;
var browserVersion = parseFloat(ua.substring(findIndex, findIndex +3));
return browserVersion;
}
var browserName = getBrowserName();
var browserVersion = getBrowserVersion();
var windowHeight = window.screen.availHeight;
var windowWidth = window.screen.availWidth;
document.write("You are using " + browserVersion + " version of the "
+ browserName + " browser to view this page in a browser whose inner" +
" window that is" + windowWidth + " pixels wide and " +
windowHeight + " pixels tall.");
<!DOCTYPE html>
<html lan= "en">
<head>
<title> Chapter 8 Browser Sniffing</title>
</head>
<body>
<script src = "browserSniffing.js"></script>
</body>
</html>