天天看点

ie和firefox的常用区别

今天在写Ajax程序时,遇到了个问题就是原理ie和firefox之间的差别原来那么大。以前写Ajax时都用的alert()来调试,这个比较麻烦但已经习惯了。我公司的师傅推荐我用firefox的firebug,看到他的使用我觉得满方便的,就回家把玩了起来。对于用惯了ie的我,习惯了用window.event来获得事件。但用firebug怎么都调不出来,后来还是用老土的方法alert(window.event)发现对象为null,从而知道firefox的差别。然后就研究了一把。

下面总结下:

  1. form对象:document.forms("FormName") firefox不支持,还是用docment.forms["FormName"]为好。我还是喜欢document.forms[0] ,毕竟有多个form的情况不多。
  2. html 对象:用document.getElementById("ID")比较通用,document.All("ID")只能用在ie.
  3. 模态对话框:showModelDialog只用ie支持,firefox只能开个新窗口window.open(url,name,parameters)。
  4. 事件:最讨厌的区别,ie用window.event得到,firefox是通过事件处理函数的参数传入。eg.οnclick="javascript:dothing(event)" 或document.forms[0].ID.οnclick=dothing,然后
  1. function dothing(event){
  2.  if(event==null)event=window.event;
  3.     if (event) {
  4.         var theTextBox = (event.target)?event.target:event.srcElement;
  5.         //得到事件源 
  6.     }
  7. }

css还有很多的差别,但到觉得不爽是再搜吧,js搞错就麻烦了。最后我备忘个ajax 通用XMLHTTP获得语句。

  1.  var req = null;
  2. if (window.XMLHttpRequest) {
  3.     req = new XMLHttpRequest();
  4. }
  5. if (!req && window.ActiveXObject)
  6. {
  7.     try
  8.     {
  9.         req = new ActiveXObject("Msxml2.XMLHTTP.5.0")
  10.     }
  11.     catch(e)
  12.     {
  13.         try
  14.         {
  15.             req = new ActiveXObject("Msxml2.XMLHTTP.4.0")
  16.         }
  17.         catch(e)
  18.         {
  19.             try
  20.             {
  21.                 req=new ActiveXObject("Msxml2.XMLHTTP")
  22.             }
  23.             catch(e)
  24.             {
  25.                 try {
  26.                    req= new ActiveXObject("Microsoft.XMLHTTP")
  27.                 } catch(e) {
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33. if (!req) {
  34.     alert("XMLHTTP不可用。即将跳转到非Ajax页面。");
  35.     location = "nonAjax.htm"
  36. }

继续阅读