今天在写Ajax程序时,遇到了个问题就是原理ie和firefox之间的差别原来那么大。以前写Ajax时都用的alert()来调试,这个比较麻烦但已经习惯了。我公司的师傅推荐我用firefox的firebug,看到他的使用我觉得满方便的,就回家把玩了起来。对于用惯了ie的我,习惯了用window.event来获得事件。但用firebug怎么都调不出来,后来还是用老土的方法alert(window.event)发现对象为null,从而知道firefox的差别。然后就研究了一把。
下面总结下:
- form对象:document.forms("FormName") firefox不支持,还是用docment.forms["FormName"]为好。我还是喜欢document.forms[0] ,毕竟有多个form的情况不多。
- html 对象:用document.getElementById("ID")比较通用,document.All("ID")只能用在ie.
- 模态对话框:showModelDialog只用ie支持,firefox只能开个新窗口window.open(url,name,parameters)。
- 事件:最讨厌的区别,ie用window.event得到,firefox是通过事件处理函数的参数传入。eg.οnclick="javascript:dothing(event)" 或document.forms[0].ID.οnclick=dothing,然后
- function dothing(event){
- if(event==null)event=window.event;
- if (event) {
- var theTextBox = (event.target)?event.target:event.srcElement;
- //得到事件源
- }
- }
css还有很多的差别,但到觉得不爽是再搜吧,js搞错就麻烦了。最后我备忘个ajax 通用XMLHTTP获得语句。
- var req = null;
- if (window.XMLHttpRequest) {
- req = new XMLHttpRequest();
- }
- if (!req && window.ActiveXObject)
- {
- try
- {
- req = new ActiveXObject("Msxml2.XMLHTTP.5.0")
- }
- catch(e)
- {
- try
- {
- req = new ActiveXObject("Msxml2.XMLHTTP.4.0")
- }
- catch(e)
- {
- try
- {
- req=new ActiveXObject("Msxml2.XMLHTTP")
- }
- catch(e)
- {
- try {
- req= new ActiveXObject("Microsoft.XMLHTTP")
- } catch(e) {
- }
- }
- }
- }
- }
- if (!req) {
- alert("XMLHTTP不可用。即将跳转到非Ajax页面。");
- location = "nonAjax.htm"
- }