跨浏览器的AJAX功能,动态加载HTML [英] Cross browser AJAX function to dynamically load HTML

查看:114
本文介绍了跨浏览器的AJAX功能,动态加载HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个AJAX功能,为动态的HTML页面。我已经发现了以下内容:

I'm looking for an AJAX function to dynamically request an HTML page. I already found the following:

function ajaxinclude(url) 
{
   var page_request = false

   if (window.XMLHttpRequest) // if Mozilla, Safari etc
      page_request = new XMLHttpRequest()
   else if (window.ActiveXObject) // if IE
   { 

     try {
       page_request = new ActiveXObject("Msxml2.XMLHTTP")
     } 
     catch (e){
       try{
         page_request = new ActiveXObject("Microsoft.XMLHTTP")
       }
       catch (e){}
     }
   }
   else
     return false

   page_request.open('GET', url, false) //get page synchronously 
   page_request.send(null)
   return page_request.responseText;
 }

它工作正常在Firefox和Chrome浏览器,但它无法在IE浏览器上以下行:

It works fine in Firefox and Chrome, but it fails in IE on the following line:

page_request.open('GET', url, false)

是否有保证是完全跨浏览器更好地提供功能兼容?

Is there a better available function that is guaranteed to be completely cross browser compatible?

编辑:感谢所有伟大的建议......到最后,我决定不再在这里重新发明轮子。和的事情,我忘了提一句的是,我还需要它来更新的间隔......虽然我已经明白这一点,所以我不认为它发挥了作用。但后来我发现,在原型大Ajax.PeriodicUpdater()方法,并极大地改变了我的脑海里。我刚刚从50 LOC解决方案参加了关于4号线:)

Thanks for all the great suggestions... in the end, I decided not to reinvent the wheel here. And one of the things I forgot to mention was that I also need it to update at an interval... though I had already figure that out so I didn't think it made a difference. But then I found the great Ajax.PeriodicUpdater() Method in prototype and vastly changed my mind. I just went from a 50 LOC solution to about 4 lines :)

推荐答案

我会同意,不要去重新发明轮子,或在这种情况下,AJAX。

I would have to agree, don't go reinventing the wheel, or in this case, AJAX.

jQuery和原型做,让你不必处理跨浏览器的支持,大大简化了JavaScript类型编程的出色的工作。我陷入了JQuery的第一次,所以我偏向它,从我所看到的库是小了一点(读:在浏览器速度更快),但原型,我相信已经有更长,且插件和例子吨在那里。 Ruby on Rails的也默认使用原型。有趣的是,code在这两个看起来非常相似,需要重写很少改变的库。

JQuery and Prototype do an excellent job of letting you NOT have to deal with cross-browser support, and greatly simplifying Javascript type programming. I fell into JQuery first so I'm biased towards it, and from what I've seen the library is a little smaller (read: faster in the browser), but Prototype I believe has been around longer and has TONS of plugins and examples out there. Ruby on Rails also by default uses Prototype. Funny enough, the code in both looks very similar and takes little rewriting to change libraries.

JQuery的教程< - 只是头部下到了AJAX节

JQuery Tutorials <-- Just head on down to the AJAX section

这篇关于跨浏览器的AJAX功能,动态加载HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆