javascript事件模型框架

原文链接

最近一直在读《javascript高级程序设计》,也快读完了,读到事件这一章,书中提供的一个事件工具类很实用,我注释了一下,并摘记:

None.gif // eventutil.js
None.gif
var  EventUtil  =   new  Object;
ExpandedBlockStart.gif
/* 
InBlock.gif  此方法用来给特定对象添加事件,oTarget是指定对象,sEventType是事件类型,如click、keydown等,    fnHandler是事件回调函数
InBlock.gif/*
InBlock.gifEventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
InBlock.gif    //firefox情况下
InBlock.gif    if (oTarget.addEventListener) {
InBlock.gif        oTarget.addEventListener(sEventType, fnHandler, false);
InBlock.gif    }
InBlock.gif    //IE下
InBlock.gif    else if (oTarget.attachEvent) {
InBlock.gif        oTarget.attachEvent("on" + sEventType, fnHandler);
InBlock.gif    }
InBlock.gif    else {
InBlock.gif        oTarget["on" + sEventType] = fnHandler;
InBlock.gif    }
InBlock.gif};
InBlock.gif/* 
InBlock.gif  此方法用来移除特定对象的特定事件,oTarget是指定对象,sEventType是事件类型,如click、keydown等,fnHandler是事件回调函数
InBlock.gif/*       
InBlock.gifEventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler) {
InBlock.gif    if (oTarget.removeEventListener) {
InBlock.gif        oTarget.removeEventListener(sEventType, fnHandler, false);
InBlock.gif    } else if (oTarget.detachEvent) {
InBlock.gif        oTarget.detachEvent("on" + sEventType, fnHandler);
InBlock.gif    } else { 
InBlock.gif        oTarget["on" + sEventType] = null;
InBlock.gif    }
InBlock.gif};
InBlock.gif
InBlock.gif/*
InBlock.gif 格式化事件,因为IE和其他浏览器下获取事件的方式不同并且事件的属性也不尽相同,通过此方法提供一个一致的事件
ExpandedBlockEnd.gif
*/

ExpandedBlockStart.gifEventUtil.formatEvent 
=   function  (oEvent)  {
InBlock.gif    
//isIE和isWin引用到一个js文件,判断浏览器和操作系统类型
ExpandedSubBlockStart.gif
    if (isIE && isWin) {
InBlock.gif        oEvent.charCode 
= (oEvent.type == "keypress"? oEvent.keyCode : 0;
InBlock.gif        
//IE只支持冒泡,不支持捕获
InBlock.gif
        oEvent.eventPhase = 2;
InBlock.gif        oEvent.isChar 
= (oEvent.charCode > 0);
InBlock.gif        oEvent.pageX 
= oEvent.clientX + document.body.scrollLeft;
InBlock.gif        oEvent.pageY 
= oEvent.clientY + document.body.scrollTop;
InBlock.gif        
//阻止事件的默认行为
ExpandedSubBlockStart.gif
        oEvent.preventDefault = function () {
InBlock.gif            
this.returnValue = false;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif         
//将toElement,fromElement转化为标准的relatedTarget 
ExpandedSubBlockStart.gif
        if (oEvent.type == "mouseout"{
InBlock.gif            oEvent.relatedTarget 
= oEvent.toElement;
ExpandedSubBlockStart.gif        }
 else if (oEvent.type == "mouseover"{
InBlock.gif            oEvent.relatedTarget 
= oEvent.fromElement;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//取消冒泡      
ExpandedSubBlockStart.gif
        oEvent.stopPropagation = function () {
InBlock.gif            
this.cancelBubble = true;
ExpandedSubBlockEnd.gif        }
;
InBlock.gif
InBlock.gif        oEvent.target 
= oEvent.srcElement;
InBlock.gif        
//添加事件发生时间属性,IE没有
InBlock.gif
        oEvent.time = (new Date).getTime();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return oEvent;
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gifEventUtil.getEvent 
=   function ()  {
ExpandedSubBlockStart.gif    
if (window.event) {
InBlock.gif        
//格式化IE的事件
InBlock.gif
        return this.formatEvent(window.event);
ExpandedSubBlockStart.gif    }
 else {
InBlock.gif        
return EventUtil.getEvent.caller.arguments[0];
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
;
None.gif


附带上一个判断浏览器和系统类型的js文件,通过引入一些名字显而易见的全局变量作为判断的结果,使用时需要小心变量名称冲突:
None.gif // detect.js,同样来自《JAVASCRIPT高级程序设计》
None.gif
var  sUserAgent  =  navigator.userAgent;
None.gif
var  fAppVersion  =  parseFloat(navigator.appVersion);
None.gif
ExpandedBlockStart.gif
function  compareVersions(sVersion1, sVersion2)  {
InBlock.gif
InBlock.gif    
var aVersion1 = sVersion1.split(".");
InBlock.gif    
var aVersion2 = sVersion2.split(".");
InBlock.gif    
ExpandedSubBlockStart.gif    
if (aVersion1.length > aVersion2.length) {
ExpandedSubBlockStart.gif        
for (var i=0; i < aVersion1.length - aVersion2.length; i++{
InBlock.gif            aVersion2.push(
"0");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gif    }
 else if (aVersion1.length < aVersion2.length) {
ExpandedSubBlockStart.gif        
for (var i=0; i < aVersion2.length - aVersion1.length; i++{
InBlock.gif            aVersion1.push(
"0");
ExpandedSubBlockEnd.gif        }
    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gif    
for (var i=0; i < aVersion1.length; i++{
InBlock.gif 
ExpandedSubBlockStart.gif        
if (aVersion1[i] < aVersion2[i]) {
InBlock.gif            
return -1;
ExpandedSubBlockStart.gif        }
 else if (aVersion1[i] > aVersion2[i]) {
InBlock.gif            
return 1;
ExpandedSubBlockEnd.gif        }
    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    
return 0;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isOpera  =  sUserAgent.indexOf( " Opera " >   - 1 ;
None.gif
var  isMinOpera4  =  isMinOpera5  =  isMinOpera6  =  isMinOpera7  =  isMinOpera7_5  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isOpera)  {
InBlock.gif    
var fOperaVersion;
ExpandedSubBlockStart.gif    
if(navigator.appName == "Opera"{
InBlock.gif        fOperaVersion 
= fAppVersion;
ExpandedSubBlockStart.gif    }
 else {
InBlock.gif        
var reOperaVersion = new RegExp("Opera (//d+//.//d+)");
InBlock.gif        reOperaVersion.test(sUserAgent);
InBlock.gif        fOperaVersion 
= parseFloat(RegExp["$1"]);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    isMinOpera4 
= fOperaVersion >= 4;
InBlock.gif    isMinOpera5 
= fOperaVersion >= 5;
InBlock.gif    isMinOpera6 
= fOperaVersion >= 6;
InBlock.gif    isMinOpera7 
= fOperaVersion >= 7;
InBlock.gif    isMinOpera7_5 
= fOperaVersion >= 7.5;
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isKHTML  =  sUserAgent.indexOf( " KHTML " >   - 1  
None.gif              
||  sUserAgent.indexOf( " Konqueror " >   - 1  
None.gif              
||  sUserAgent.indexOf( " AppleWebKit " >   - 1
None.gif              
None.gif
var  isMinSafari1  =  isMinSafari1_2  =   false ;
None.gif
var  isMinKonq2_2  =  isMinKonq3  =  isMinKonq3_1  =  isMinKonq3_2  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isKHTML)  {
InBlock.gif    isSafari 
= sUserAgent.indexOf("AppleWebKit"> -1;
InBlock.gif    isKonq 
= sUserAgent.indexOf("Konqueror"> -1;
InBlock.gif
ExpandedSubBlockStart.gif    
if (isSafari) {
InBlock.gif        
var reAppleWebKit = new RegExp("AppleWebKit///(//d+(?://.//d*)?)");
InBlock.gif        reAppleWebKit.test(sUserAgent);
InBlock.gif        
var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
InBlock.gif
InBlock.gif        isMinSafari1 
= fAppleWebKitVersion >= 85;
InBlock.gif        isMinSafari1_2 
= fAppleWebKitVersion >= 124;
ExpandedSubBlockStart.gif    }
 else if (isKonq) {
InBlock.gif
InBlock.gif        
var reKonq = new RegExp("Konqueror///(//d+(?://.//d+(?://.//d)?)?)");
InBlock.gif        reKonq.test(sUserAgent);
InBlock.gif        isMinKonq2_2 
= compareVersions(RegExp["$1"], "2.2">= 0;
InBlock.gif        isMinKonq3 
= compareVersions(RegExp["$1"], "3.0">= 0;
InBlock.gif        isMinKonq3_1 
= compareVersions(RegExp["$1"], "3.1">= 0;
InBlock.gif        isMinKonq3_2 
= compareVersions(RegExp["$1"], "3.2">= 0;
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isIE  =  sUserAgent.indexOf( " compatible " >   - 1  
None.gif           
&&  sUserAgent.indexOf( " MSIE " >   - 1
None.gif           
&&   ! isOpera;
None.gif           
None.gif
var  isMinIE4  =  isMinIE5  =  isMinIE5_5  =  isMinIE6  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isIE)  {
InBlock.gif    
var reIE = new RegExp("MSIE (//d+//.//d+);");
InBlock.gif    reIE.test(sUserAgent);
InBlock.gif    
var fIEVersion = parseFloat(RegExp["$1"]);
InBlock.gif
InBlock.gif    isMinIE4 
= fIEVersion >= 4;
InBlock.gif    isMinIE5 
= fIEVersion >= 5;
InBlock.gif    isMinIE5_5 
= fIEVersion >= 5.5;
InBlock.gif    isMinIE6 
= fIEVersion >= 6.0;
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isMoz  =  sUserAgent.indexOf( " Gecko " >   - 1
None.gif            
&&   ! isKHTML;
None.gif
None.gif
var  isMinMoz1  =  sMinMoz1_4  =  isMinMoz1_5  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isMoz)  {
InBlock.gif    
var reMoz = new RegExp("rv:(//d+//.//d+(?://.//d+)?)");
InBlock.gif    reMoz.test(sUserAgent);
InBlock.gif    isMinMoz1 
= compareVersions(RegExp["$1"], "1.0">= 0;
InBlock.gif    isMinMoz1_4 
= compareVersions(RegExp["$1"], "1.4">= 0;
InBlock.gif    isMinMoz1_5 
= compareVersions(RegExp["$1"], "1.5">= 0;
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isNS4  =   ! isIE  &&   ! isOpera  &&   ! isMoz  &&   ! isKHTML 
None.gif            
&&  (sUserAgent.indexOf( " Mozilla " ==   0
None.gif            
&&  (navigator.appName  ==   " Netscape "
None.gif            
&&  (fAppVersion  >=   4.0   &&  fAppVersion  <   5.0 );
None.gif
None.gif
var  isMinNS4  =  isMinNS4_5  =  isMinNS4_7  =  isMinNS4_8  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isNS4)  {
InBlock.gif    isMinNS4 
= true;
InBlock.gif    isMinNS4_5 
= fAppVersion >= 4.5;
InBlock.gif    isMinNS4_7 
= fAppVersion >= 4.7;
InBlock.gif    isMinNS4_8 
= fAppVersion >= 4.8;
ExpandedBlockEnd.gif}

None.gif
None.gif
var  isWin  =  (navigator.platform  ==   " Win32 " ||  (navigator.platform  ==   " Windows " );
None.gif
var  isMac  =  (navigator.platform  ==   " Mac68K " ||  (navigator.platform  ==   " MacPPC "
None.gif            
||  (navigator.platform  ==   " Macintosh " );
None.gif
None.gif
var  isUnix  =  (navigator.platform  ==   " X11 " &&   ! isWin  &&   ! isMac;
None.gif
None.gif
var  isWin95  =  isWin98  =  isWinNT4  =  isWin2K  =  isWinME  =  isWinXP  =   false ;
None.gif
var  isMac68K  =  isMacPPC  =   false ;
None.gif
var  isSunOS  =  isMinSunOS4  =  isMinSunOS5  =  isMinSunOS5_5  =   false ;
None.gif
ExpandedBlockStart.gif
if  (isWin)  {
InBlock.gif    isWin95 
= sUserAgent.indexOf("Win95"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows 95"> -1;
InBlock.gif    isWin98 
= sUserAgent.indexOf("Win98"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows 98"> -1;
InBlock.gif    isWinME 
= sUserAgent.indexOf("Win 9x 4.90"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows ME"> -1;
InBlock.gif    isWin2K 
= sUserAgent.indexOf("Windows NT 5.0"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows 2000"> -1;
InBlock.gif    isWinXP 
= sUserAgent.indexOf("Windows NT 5.1"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows XP"> -1;
InBlock.gif    isWinNT4 
= sUserAgent.indexOf("WinNT"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows NT"> -1 
InBlock.gif              
|| sUserAgent.indexOf("WinNT4.0"> -1 
InBlock.gif              
|| sUserAgent.indexOf("Windows NT 4.0"> -1 
InBlock.gif              
&& (!isWinME && !isWin2K && !isWinXP);
ExpandedBlockEnd.gif}
 
None.gif
ExpandedBlockStart.gif
if  (isMac)  {
InBlock.gif    isMac68K 
= sUserAgent.indexOf("Mac_68000"> -1 
InBlock.gif               
|| sUserAgent.indexOf("68K"> -1;
InBlock.gif    isMacPPC 
= sUserAgent.indexOf("Mac_PowerPC"> -1 
InBlock.gif               
|| sUserAgent.indexOf("PPC"> -1;  
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gif
if  (isUnix)  {
InBlock.gif    isSunOS 
= sUserAgent.indexOf("SunOS"> -1;
InBlock.gif
ExpandedSubBlockStart.gif    
if (isSunOS) {
InBlock.gif        
var reSunOS = new RegExp("SunOS (//d+//.//d+(?://.//d+)?)");
InBlock.gif        reSunOS.test(sUserAgent);
InBlock.gif        isMinSunOS4 
= compareVersions(RegExp["$1"], "4.0">= 0;
InBlock.gif        isMinSunOS5 
= compareVersions(RegExp["$1"], "5.0">= 0;
InBlock.gif        isMinSunOS5_5 
= compareVersions(RegExp["$1"], "5.5">= 0;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值