首頁 >web前端 >js教程 >(跨瀏覽器基礎事件/瀏覽器偵測/判斷瀏覽器)經驗程式碼分享_javascript技巧

(跨瀏覽器基礎事件/瀏覽器偵測/判斷瀏覽器)經驗程式碼分享_javascript技巧

WBOY
WBOY原創
2016-05-16 17:43:05991瀏覽
跨瀏覽器基礎事件
複製程式碼 程式碼如下:


程式碼如下:


/跨瀏覽器新增事件
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);
obj.removeEventListener(type, fn, false);
} else if (objse); .detachEvent) {
obj.detachEvent('on' type, fn);
}
}
//跨瀏覽器阻止預設行為
function preDef(evt) {
var e = evt || window.event;
if (e.preventDefault) {
e.preventDefault();
} else {
e.returnValue = false;
}
}
e. >}
//跨瀏覽器取得目標物件
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,
web? >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🎜>safariVersion = 1;
} else if🎜>safariVersion = 1;
} else if🎜>safariVersion = 1;
} else if🎜>safariVersion = 1;
} else if 。 🎜>}
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 ]。
var dataProp = data[i].prop;
this.versionSearchString = data[i].versionSearch ||資料[i].身分;
if (dataString) {
if (dataString.indexOf(data[i].subString) != -1 )
返回資料[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: 導航器。 >身分: "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 ",
身份: "Netscape"
},
{
字串:navigator.userAgent,
子字串:「MSIE」,
身分:「Internet Explorer ”,
versionSearch:“MSIE”
},
{
string: navigator.userAgent,
subString: "Gecko",
identity: "Milla", Search : " 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();
警報(BrowserDetect.browser); //名稱
alert(BrowserDetect.version); //版本
alert(BrowserDetect.OS) //系統

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn