首页 > web前端 > js教程 > 正文

(跨浏览器基础事件/浏览器检测/判断浏览器)经验代码分享_javascript技巧

WBOY
发布: 2016-05-16 17:43:05
原创
969 人浏览过
跨浏览器基础事件
复制代码 代码如下:

//跨浏览器添加事件
function addEvent(obj, type, fn) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
obj.attachEvent('on' type, function () {
fn.call(obj);
});
}
}
//跨浏览器基础事件
function removeEvent(obj, type, fn) {
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent('on' type, fn);
}
}
//跨浏览器阻止默认行为
function preDef(evt) {
var e = evt || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
//跨浏览器获取目标对象
function getTarget(evt) {
if (evt.target) { //W3C
return evt.target;
} else if (window.event.srcElement) { //IE
return window.event.srcElement;
}
}
//跨浏览器获取字符编码
function getCharCode(evt) {
var e = evt || window.event;
if (typeof e.charCode == 'number') {
return e.charCode;
} else {
return e.keyCode;
}
}

浏览器检测
复制代码 代码如下:

var client = function(){
//渲染引擎
var engine = {
ie: 0,
gecko: 0,
webkit: 0,
khtml: 0,
opera: 0,
//完整版
ver: null
};
//浏览器
var browser = {
//浏览器
ie: 0,
firefox: 0,
safari: 0,
konq: 0,
opera: 0,
chrome: 0,
//特定版本
ver: null
};
//平台/设备/操作系统
var system = {
win: false,
mac: false,
x11: false,
//移动设备
iphone: false,
ipod: false,
ipad: false,
ios: false,
android: false,
nokiaN: false,
winMobile: false,
//游戏系统
wii: false,
ps: false
};
//检测渲染引擎/浏览器
var ua = navigator.userAgent;
if (window.opera){
engine.ver = browser.ver = window.opera.version();
engine.opera = browser.opera = parseFloat(engine.ver);
} else if (/AppleWebKit/(S )/.test(ua)){
engine.ver = RegExp["$1"];
engine.webkit = parseFloat(engine.ver);
//判断是 Chrome 还是 Safari
if (/Chrome/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.chrome = parseFloat(browser.ver);
} else if (/Version/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.safari = parseFloat(browser.ver);
} else {
//大概版本
var safariVersion = 1;
if (engine.webkit safariVersion = 1;
} else if (engine.webkit safariVersion = 1.2;
} else if (engine.webkit safariVersion = 1.3;
} 其他 {
safariVersion = 2;
}
browser.safari = browser.ver = safariVersion;
}
} else if (/KHTML/(S )/.test(ua) || /Konqueror/([^;] )/.test(ua)){
engine.ver = browser .ver = RegExp["$1"];
engine.khtml = browser.konq = parseFloat(engine.ver);
} else if (/rv:([^)] )) Gecko/d{8}/.test(ua)){
engine.ver = RegExp["$1"];
engine.gecko = parseFloat(engine.ver);
//判断是否是Firefox
if (/Firefox/(S )/.test(ua)){
browser.ver = RegExp["$1"];
browser.firefox = parseFloat(browser.ver);
}
} else if (/MSIE ([^;] )/.test(ua)){
engine.ver = browser.ver = RegExp["$1"];
engine.ie = browser.ie = parseFloat(engine.ver);
}
//检测浏览器
browser.ie = engine.ie;
browser.opera = engine.opera;
//检测平台
var p = navigator.platform;
system.win = p.indexOf("Win") == 0;
system.mac = p.indexOf("Mac") == 0;
system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
//检测windows操作系统
if (system.win){
if (/Win(?:dows )?([^do]{2})s?(d .d )?/ .test(ua)){
if (RegExp["$1"] == "NT"){
switch(RegExp["$2"]){
case "5.0":
系统.win = "2000";
休息;
案例“5.1”:
system.win =“XP”;
休息;
案例“6.0”:
system.win =“Vista”;
休息;
案例“6.1”:
system.win =“7”;
休息;
默认:
system.win = "NT";
休息;
}
} else if (RegExp["$1"] == "9x"){
system.win = "ME";
} else {
system.win = RegExp["$1"];
}
}
}
//移动设备
system.iphone = ua.indexOf("iPhone") >; -1;
system.ipod = ua.indexOf("iPod") > -1;
system.ipad = ua.indexOf("iPad") > -1;
system.nokiaN = ua.indexOf("NokiaN") > > -1;
//windows mobile
if (system.win == "CE"){
system.winMobile = system.win;
} else if (system.win == "Ph"){
if(/Windows Phone OS (d .d )/.test(ua)){;
system.win = "电话";
system.winMobile = parseFloat(RegExp["$1"]);
}
}
//判断iOS版本
if (system.mac && ua.indexOf("Mobile") > -1){
if (/CPU (?:iPhone )?OS (d _d )/.test(ua)){
system.ios = parseFloat(RegExp.$1.replace("_", "."));
} 其他 {
system.ios = 2; //无法真正检测到 - 所以猜测
}
}
//确定 Android 版本
if (/Android (d .d )/.test(ua)){
system .android = parseFloat(RegExp.$1);
}
//游戏系统
system.wii = ua.indexOf("Wii") >; -1;
system.ps = /playstation/i.test(ua);
//返回
return {
engine: 引擎,
browser: 浏览器,
system: 系统
};
}();

判断浏览器的名称、版本号、网络
复制代码 代码如下:

var BrowserDetect = {
init: function () {
this.browser = this.searchString(this.dataBrowser) || “未知的浏览器”;
this.version = this.searchVersion(navigator.userAgent)
|| this.searchVersion(navigator.appVersion)
|| “未知版本”;
this.OS = this.searchString(this.dataOS) || “未知的操作系统”;
},
searchString: 函数 (数据) {
for (var i=0;ivar dataString = data[i].string;
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch ||数据[i].身份;
if (dataString) {
if (dataString.indexOf(data[i].subString) != -1)
return data[i].identity;
}
else if (dataProp)
返回 data[i].identity;
}
},
searchVersion: function (dataString) {
var index = dataString.indexOf(this.versionSearchString);
if(索引==-1)返回;
return parseFloat(dataString.substring(index this.versionSearchString.length 1));
},
dataBrowser: [
{
字符串: navigator.userAgent,
subString: "Chrome",
身份: "Chrome"
},
{ string: navigator.userAgent,
subString: "OmniWeb",
versionSearch: "OmniWeb/",
identity: "OmniWeb"
},
{
string: navigator.vendor ,
subString: "Apple",
identity: "Safari",
versionSearch: "版本"
},
{
prop: window.opera,
identity: "Opera",
versionSearch: "版本"
},
{
字符串: navigator.vendor,
subString: "iCab",
身份: "iCab"
},
{
字符串: navigator.vendor,
子字符串: "KDE",
身份: "Konqueror"
},
{
字符串: navigator.userAgent,
子字符串:“Firefox”,
身份:“Firefox”
},
{
字符串:navigator.vendor,
子字符串:“卡米诺”,
身份:“ Camino"
},
{ // 对于较新的 Netscape (6 )
string: navigator.userAgent,
subString: "Netscape",
identity: "Netscape"
},
{
字符串:navigator.userAgent,
子字符串:“MSIE”,
身份:“Internet Explorer”,
versionSearch:“MSIE”
},
{
string: navigator.userAgent,
subString: "Gecko",
identity: "Mozilla",
versionSearch: "rv"
},
{ // 对于较旧的 Netscape (4 -)
字符串:navigator.userAgent,
子字符串:“Mozilla”,
身份:“Netscape”,
版本搜索:“Mozilla”
}
],
dataOS : [
{
字符串: navigator.platform,
subString: "Win",
身份: "Windows"
},
{
字符串: navigator.platform,
subString:“Mac”,
身份:“Mac”
},
{
字符串:navigator.userAgent,
subString:“iPhone”,
身份:“ iPhone/iPod"
},
{
字符串: navigator.platform,
子字符串: "Linux",
身份: "Linux"
}
]
};
BrowserDetect.init();
alert(BrowserDetect.browser); //名称
alert(BrowserDetect.version); //版本
alert(BrowserDetect.OS) //系统
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!