一切福田,不離方寸,從心而覓,感無不通。

Category Archives: JavaScript

firefox下window.event的解决方法

 在FireFox下编写事件处理函数是很麻烦的事. 因为FireFox并没有 window.event . 如果要得到 event 对象,就必须要声明时间处理函数的第一个参数为event. 所以为了兼容IE与FireFox,一般的事件处理方法为:btn.onclick=handle_btn_click;function handle_btn_click(evt){    if(evt==null)evt=window.event;//IE    //处理事件.}对于简单的程序,这不算麻烦. 但对于一些复杂的程序,某写函数根本就不是直接与事件挂钩的.如果要把event传进该参数,那么所有的方法都要把event传来传去..这简直就是噩梦. 下面介绍一个解决这个麻烦事的方法,与原理. JScript中,函数的调用是有一个 func.caller 这个属性的.例如 function A(){    B();}function B(){    alert(B.caller);}如果B被A调用,那么B.caller就是A 另外,函数有一个arguments属性. 这个属性可以遍历函数当前执行的参数:function myalert(){    var arr=[];    for(var i=0;i        arr[i]=myalert.arguments[i];    alert(arr.join("-"));}alert("hello","world",1,2,3)就能显示 hello-world-1-2-3(arguments的个数与调用方有关,而与函数的参数定义没有任何关系) 根据这两个属性,我们可以得到第一个函数的event对象:btn.onclick=handle_click;function handle_click(){    showcontent();}function showcontent(){    var evt=SearchEvent();    if(evt&&evt.shiftKey)//如果是基于事件的调用,并且shift被按下        window.open(global_helpurl);    else        location.href=global_helpurl;}function SearchEvent(){    func=SearchEvent.caller;    while(func!=null)    {        var arg0=func.arguments[0];        if(arg0)        {            if(arg0.constructor==Event) // 如果就是event 对象                return arg0;        }        func=func.caller;    }    return null;}这个例子使用了SearchEvent来搜索event对象. 其中 'Event' 是 FireFox 的 event.constructor .在该例子运行时,SearchEvent.caller就是showcontent,但是showcontent.arguments[0]是空.所以 func=func.caller 时,func变为handle_click .handle_click 被 FireFox 调用, 虽然没有定义参数,但是被调用时,第一个参数就是event,所以handle_click.arguments[0]就是event ! 针对上面的知识,我们可以结合 prototype.__defineGetter__ 来实现 window.event 在 FireFox 下的实现: 下面给出一个简单的代码.. 有兴趣的可以补充 if(window.addEventListener){ FixPrototypeForGecko();}function FixPrototypeForGecko(){ HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle); window.constructor.prototype.__defineGetter__("event",window_prototype_get_event); Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);}function element_prototype_get_runtimeStyle(){ //return style instead… return […]

龙生   16 Dec 2011
View Details

window.event对象详细介绍

 1、event代表事件的状态,例如触发event对象的元素、鼠标的位置及状态、按下的键等等。event对象只在事件发生的过程中才有效。event的某些属性只对特定的事件有意义。比如,fromElement 和 toElement 属性只对 onmouseover 和 onmouseout 事件有意义。  2、属性: altKey, button, cancelBubble, clientX, clientY, ctrlKey, fromElement, keyCode, offsetX, offsetY, propertyName, returnValue, screenX, screenY, shiftKey, srcElement, srcFilter, toElement, type, x, y   3、属性详细说明: 属性名 描述 值 说明 altKey 检查alt键的状态 当alt键按下时,值为True否则为False 只读 shiftKey 检查shift键的状态 当shift键按下时,值为True否则为False 只读 ctrlKey 检查ctrl键的状态 当ctrl键按下时,值为True否则为False 只读 例:(点击按钮时显示那几个特殊键按下的状态) <input type="button" value="点击" onClick="showState()"/> <script> function show(){  alert("altKey:"+window.event.altKey   +"\nshiftKey:"+window.event.shiftKey   +"\nctrlKey:"+window.event.ctrlKey); }</script>  keyCode  检测键盘事件相对应的内码    可读写,可以是任何一个Unicode键盘内码。如果没有引发键盘事件,则该值为0 例:(按回车键让下一组件得到焦点,相当按Tab键) <input type="text" onKeyDown="nextBlur()"/> <input type="text"/> <script> function nextBlur(){  if(window.event.keyCode==13)//回车键的 code   window.event.keyCode=9;//Tab键的code } </script>  srcElement  返回触发事件的元素  Object  只读 例:(点击按钮时显示按钮的name值) <input type="button" value="闽" name="福建" onClick="show()"/> […]

龙生   16 Dec 2011
View Details

JavaScript判断浏览器类型及版本

 你知道世界上有多少种浏览器吗?除了我们熟知的IE, Firefox, Opera, Safari四大浏览器之外,世界上还有近百种浏览器。        几天前,浏览器家族有刚诞生了一位小王子,就是Google推出的Chrome浏览器。由于Chrome出生名门,尽管他还是个小家伙,没有人敢小看他。以后,咱们常说浏览器的“四大才子”就得改称为“五朵金花”了。        在网站前端开发中,浏览器兼容性问题本已让我们手忙脚乱,Chrome的出世不知道又要给我们添多少乱子。浏览器兼容性是前端开发框架要解决的第一个问题,要解决兼容性问题就得首先准确判断出浏览器的类型及其版本。        JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本。JavaScript判断浏览器类型一般有两种办法,一种是根据各种浏览器独有的属性来分辨,另一种是通过分析浏览器的userAgent属性来判断的。在许多情况下,值判断出浏览器类型之后,还需判断浏览器版本才能处理兼容性问题,而判断浏览器的版本一般只能通过分析浏览器的userAgent才能知道。        我们先来分析一下各种浏览器的特征及其userAgent。        IE       只有IE支持创建ActiveX控件,因此她有一个其他浏览器没有的东西,就是ActiveXObject函数。只要判断window对象存在ActiveXObject函数,就可以明确判断出当前浏览器是IE。而IE各个版本典型的userAgent如下:          Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)        Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)        Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)        Mozilla/4.0 (compatible; MSIE 5.0; Windows NT)       其中,版本号是MSIE之后的数字。        Firefox        Firefox中的DOM元素都有一个getBoxObjectFor函数,用来获取该DOM元素的位置和大小(IE对应的中是getBoundingClientRect函数)。这是Firefox独有的,判断它即可知道是当前浏览器是Firefox。Firefox几个版本的userAgent大致如下:         Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1        Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3        Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12      其中,版本号是Firefox之后的数字。        Opera        Opera提供了专门的浏览器标志,就是window.opera属性。Opera典型的userAgent如下:          Opera/9.27 (Windows NT 5.2; U; zh-cn)        […]

龙生   14 Dec 2011
View Details

通过user-Agent获取浏览器和操作系统信息

 下面是我整理的一些userAgent大家先看看格式 //Firefox 1.0.XMozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7//Firefox 1.XMozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20051111 Firefox/1.5Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8) Gecko/20060109 Firefox/1.5 (pigfoot) Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060308 Firefox/1.5.0.2Mozilla/5.0 (Windows; U; […]

龙生   14 Dec 2011
View Details

判断一个js对象是不是数组

  function isArray(o) {       return Object.prototype.toString.call(o) === '[object Array]';      }     判断更多特殊类型:  var is = {         types : ["Array","RegExp","Date","Number","String","Object","HTMLDocument"]     };     for(var i=0,c;c=is.types[i++];){         is[c] = (function(type){             return function(obj){                 return Object.prototype.toString.call(obj) == “[object "+type+"]“;             }         })(c);     }   

龙生   11 Nov 2011
View Details

JS 设置为首页和加入收藏夹

  设置为首页:  <a href="#" onClick="this.style.behavior=’url(#default#homepage)';this.setHomePage('这里面写上你自己主页就可以了')">设置为首页</a>     个人理解:其中behavior是css属性,设置url为ie默认(default)动作。setHomePage是设置或返回主页的路径。   加入收藏夹 <a  href="javascript:window.external.AddFavorite('这里面写上要加入收藏夹的地址就可以了', '收藏夹里显示的文字描述信息');">加入收藏夹</a> 

龙生   21 Apr 2011
View Details

window.event.keyCode兼容Firefox

1.window.event.keyCode的用法:    例子、<input type="text" onkeydown="javaScript:if(window.event.keyCode==13) kenNumIE();" />  <script language="javascript" type="text/javascript">  function kenNumIE(){  alert("你按的是回车键!");  }  </script>  但是该方法只对IE有效,原因是Firefox的window对象没有event属性,所以window.event是不存在的,Firefox只能在事件句柄函数的第一个参数获取事件对象。2.兼容Firefox的方法:<input type="text" onkeydown="return keyNumAll(event);" >  <script language="javascript" type="text/javascript">  function keyNumAll(evt){  var k=window.event?evt.keyCode:evt.which;  if(k == 13){  alert("你按的是回车键!");  }  }  </script>  这样,当你按回车键时,就可以触发keyNumAll(event)事件了。 附:window.event.keyCode 集合(键盘字符的Ascii): XML/HTML代码 Ascii 代码表   Ascii 0 {Nulo, Sem Som}   Ascii 1   Ascii 2   Ascii 3   Ascii 4   Ascii 5   Ascii 6   Ascii 7   Ascii 8 {BackSpace}   Ascii 9 {Tab}   Ascii 10   Ascii 11   Ascii 12   Ascii 13 {Enter}   Ascii 14   Ascii 15   Ascii 16 {Shift}   Ascii 17 {CTRL}   Ascii 18 {ALT}   Ascii 19   Ascii 20 {CapsLock}   Ascii 21   Ascii 22   Ascii 23   Ascii 24   Ascii 25   Ascii 26   Ascii 27   Ascii 28   Ascii 29   – Ascii 30   – Ascii 31   Ascii 32 {Espa?o}   ! Ascii 33   " Ascii 34   # Ascii 35   $ Ascii 36   % Ascii 37   & Ascii 38   ' Ascii 39   ( Ascii 40   ) Ascii 41   * Ascii 42   + Ascii 43   , Ascii 44   – Ascii 45   . Ascii 46   / Ascii 47   0 Ascii 48   1 Ascii 49   […]

龙生   04 Jan 2011
View Details

禁止选择功能实现代码(兼容IE/Firefox/Chrome)

ie,chrome可能通过JS的onselectstart 例如代码如下:<body onselectstart="return false"> firefox可以通过CSS 代码如下:  *{ -moz-user-select:none }

龙生   05 Nov 2010
View Details

web打印控件与分页

最重要的三段代码如下: view plaincopy to clipboardprint?01.<mce:script type="text/javascript"><!--   02.      03.      var HKEY_Root,HKEY_Path,HKEY_Key;       04.      HKEY_Root="HKEY_CURRENT_USER";       05.      HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";       06.          //设置网页打印的页眉页脚为空       07.      function PageSetup_Null()      08.       {       09.         try {       10.                 var Wsh=new ActiveXObject("WScript.Shell");       11.         HKEY_Key="header";       12.         Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");       13.         HKEY_Key="footer";       14.         Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");       15.         }  catch(e){}       16.       }       17.       //恢复网页打印的页眉页脚      18.       function PageSetup_default(){   19.        try {   20.         var Wsh=new ActiveXObject("WScript.Shell");       21.          HKEY_Key="header";       22.          Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&w&b页码,&p/&P");       23.          HKEY_Key="footer";       24.          Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"&u&b&d");   25.          }catch(e){}   26.         }   27.        function printsetup(){    28.    // 打印页面设置    29.    wb.execwb(8,1);    30.   }    31.   function printpreview(){    32.    // 打印页面预览    33.    PageSetup_Null();    34.    wb.execwb(7,1);    35.   }    […]

龙生   03 Sep 2010
View Details

javascript获取屏幕,浏览器,网页高度宽度

浏览器窗口宽:document.documentElement.clientWidth 浏览器窗口高:document.documentElement.clientHeight 网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWidth (包括边线的宽) 网页可见区域高:document.body.offsetHeight (包括边线的宽) 网页正文全文宽:document.body.scrollWidth 网页正文全文高:document.body.scrollHeight 网页被卷去的高:document.body.scrollTop 网页被卷去的左:document.body.scrollLeft 网页正文部分上:window.screenTop 网页正文部分左:window.screenLeft 屏幕分辨率的高:window.screen.height 屏幕分辨率的宽:window.screen.width 屏幕可用工作区高度:window.screen.availHeight 屏幕可用工作区宽度:window.screen.availWidth HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度。 scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离 scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离 scrollWidth:获取对象的滚动宽度 offsetHeight:获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度 offsetLeft:获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置 offsetTop:获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置 event.clientX 相对文档的水平座标 event.clientY 相对文档的垂直座标 event.offsetX 相对容器的水平坐标 event.offsetY 相对容器的垂直坐标 document.documentElement.scrollTop 垂直方向滚动的值 event.clientX+document.documentElement.scrollTop 相对文档的水平座标+垂直方向滚动的量 IE,FireFox 差异如下: IE6.0、FF1.06+: clientWidth = width + padding clientHeight = height + padding offsetWidth = width + padding + border offsetHeight = height + padding + border IE5.0/5.5: clientWidth = width – border clientHeight = height – border offsetWidth = width offsetHeight = […]

龙生   09 Jun 2010
View Details