js检测浏览器类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js检测浏览器类型相关的知识,希望对你有一定的参考价值。
想要一段可以检测浏览器的JS代码
只要检测出浏览器是否是(火狐浏览器),如果是则跳出一段提示(提示可以自定义),如果不是火狐浏览器则不作任何提示,
各位有这种代码的朋友分享一段出来吧!谢谢了
其他类型的浏览器都不跳出窗口,只要火狐浏览器才跳出窗口??
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="YangHui">
<META NAME="Keywords" CONTENT="YangHui">
<META NAME="Description" CONTENT="YangHui">
<SCRIPT LANGUAGE="javascript">
<!--
/**
* Navigator对象的全部属性信息
*/
function navigatorInfoAll()
var msg = "浏览器信息如下\n";
for (var propname in navigator)
msg += propname + ":" + navigator[propname] + "\n";
alert(msg);
//navigatorInfoAll();
/**
* Navigator对象的部分属性信息
*/
function navigatorInfos()
var msg = "浏览器信息如下\n";
msg += "appName:" + navigator["appName"] + "\n";
msg += "appVersion:" + navigator["appVersion"] + "\n";
msg += "userAgent:" + navigator["userAgent"] + "\n";
msg += "appCodeName:" + navigator["appCodeName"] + "\n";
msg += "platform:" + navigator["platform"] + "\n";
alert(msg);
//navigatorInfos();
/**
* Navigator对象的属性实例
*/
function navigatorSimpleVersion()
var msg = "当前使用的浏览器为:\n";
var browser = new Object();
browser.version = parseInt(navigator["appVersion"]);//browser.version = parseInt(navigator.appVersion);
browser.isNetscape = false;//Netscape Browser
browser.isMicrosoft = false;//Microsoft Browser
if (navigator["appName"].indexOf("Netscape") != -1)
browser.isNetscape = true;
msg += navigator["appName"];
if (navigator["appName"].indexOf("Microsoft") != -1)
browser.isMicrosoft = true;
msg += navigator["appName"];
alert(msg);
navigatorSimpleVersion();
//-->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML> 参考技术A // 封装判断浏览器类型和版本信息的类LB (全名LBrowser)
var LB =
appName : null,// 浏览器类型
lUserAgent : null,
userAgent : navigator.userAgent,
appVersion : null, // 浏览器版本
fullApp : null,// 类型+版本
apps:["ie","firefox","opera","safari","chrome"],
I:"ie",F:"firefox",O:"opera",S:"safari",C:"chrome",
getAppName : function()
this.appName = navigator.appName;
return this.appName;
,
getLUserAgent : function()
this.lUserAgent = navigator.userAgent.toLowerCase();
// this.lUserAgent = "mozilla/5.0 (windows; u; windows nt 5.2) gecko/2008070208 firefox/3.0.1";
// this.lUserAgent = "mozilla/5.0 (macintosh; ppc mac os x; u; en) opera 8.0";
// this.lUserAgent = "mozilla/5.0 (windows; u; windows nt 5.2) applewebkit/525.13 (khtml, like gecko) version/3.1 safari/525.13";
// this.lUserAgent = "opera/8.0 (macintosh; ppc mac os x; u; en)";
// this.lUserAgent = "mozilla/5.0 (windows; u; windows nt 5.2) gecko/2008070208 test/3.0.1";
return this.lUserAgent;
,
whichApp : function()
var lu = this.getLUserAgent();
if(this.isie())
return this.appName = "ie";
else if(lu.indexOf('firefox') != -1) // not contains getBoxObjectFor always
return this.appName = "firefox";
else if(window.opera || (lu.indexOf('opera') != -1 && lu.indexOf('chrome') == -1)) // not contains chrome
return this.appName = "opera";
else if((lu.indexOf('safari') != -1 && lu.indexOf('version') != -1)) // chrome : window.openDatabase also
return this.appName = "safari";
else if(lu.indexOf('chrome') != -1)// contains opera
return this.appName = "chrome";
else
throw new Error("Sorry ! Check The AppName Failure ! Please Update The 'LB' Plugin !");
return this.appName = null;
,
whichVersion : function()
trythis.whichApp();catch(ex)
if("ie" == this.appName)
var reg = /msie\\s+[\\d\\.]+/gi;
return this.appVersion = this.lUserAgent.match(reg)[0].split(" ")[1];
else if("firefox" == this.appName)
var reg = /firefox\\s*\\/[\\d\\.]+/gi;
return this.appVersion = this.lUserAgent.match(reg)[0].split("/")[1];
else if("opera" == this.appName)
var reg = /opera(\\/)?\\s*[\\d\\.]+/gi;
return this.appVersion = this.lUserAgent.match(reg)[0].indexOf('/') != -1 ? this.lUserAgent.match(reg)[0].split("/")[1] : this.lUserAgent.match(reg)[0].split(" ")[1];
else if("chrome" == this.appName)
var reg = /chrome\\s*\\/[\\d\\.]+/gi;
return this.appVersion = this.lUserAgent.match(reg)[0].split("/")[1];
else if("safari" == this.appName)
var reg = /version\\s*\\/[\\d\\.]+/gi;
return this.appVersion = this.lUserAgent.match(reg)[0].split("/")[1];
else
throw new Error("Sorry ! Check The AppVersion Failure ! Please Update The 'LB' Plugin !");
return this.appVersion = null;
,
isie : function()
return null != window.ActiveXObject;
,
init : function()
this.getLUserAgent();
try
this.whichApp();
this.whichVersion();
catch(ex)
alert(ex);
this.fullApp = this.appName + this.appVersion;
;
<body onload="LB.init();">
</body> 参考技术B <script>
var cb = "Unknown";
if(navigator.userAgent.toLowerCase().indexOf("firefox") != -1)
cb = "Firefox";
alert( cb);
</script>本回答被提问者采纳
JS number数字类型
number:正数、负数、0、小数、NaN
NaN:not a number 不是一个有效数字 但是 他是一个属于number数据类型的
var num = 12;
12==12 比较
var a= 12 赋值;
isNaN();检测一个值是否为有效数字的命题是否成立,是有效数字返回false,不是有效数字返回的才是true;
如果检测的值不是number类型的,浏览器会默认的把它转化成number类型,然后在判断是否为有效的数字
console.log(isNaN("123")) 返回结果是false
先把"123"转化成number类型的Number ==》Number("123")
Number()强制将其他的数据类型转换为number类型,要求如果是字符串,字符串中一定都需要是数字才可以转换
Number("12")==>12 Number("12px")==>NaN
非强制数据类型转换parseInt/parseFloat
console.log(parseInt("12px"));
从左到右,一个个字符查找,把是数字的转换为有效数字,中途如果遇到了一个非有效数字,就不在继续查找了
parseFloat()同上
以上是关于js检测浏览器类型的主要内容,如果未能解决你的问题,请参考以下文章