开发者

AJAX object in Safari and Chrome?

开发者 https://www.devze.com 2022-12-11 00:56 出处:网络
I asked, Can you tell me why my searches are working ok in IE8 but get stuck with safari and chrome? www.netivot.biz

I asked,

Can you tell me why my searches are working ok in IE8 but get stuck with safari and chrome?

www.netivot.biz

The ajax code is at www.netivot.biz/js/Ajax.js

It works with some xml and xslt files

then jitter suggested to preplace my code with :

function getAjaxObject() {
  var xmlHttp = null;  
  try {    
    //FF, Opera, Safari, Chrome, IE7+
    xmlHttp = new XMLHttpRequest();  
 } catch(e) {    
     try {      
       //IE6+
          xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");    
     } catch(e) {
          try {
            //IE5+
            xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
          } catch(e) {
            xmlHttp  = null;
          }
     }
 }  
 return 开发者_如何学GoxmlHttp;}

but still dont work on safari and chrome any advise?


Try this one out, it has the benefit of not being browser dependent.

 // Browser-agnostic factory function
 _createXMLHttpRequest: function() {
   if (window.XMLHttpRequest) {
     return new XMLHttpRequest();
   } else if (window.ActiveXObject) {
     return new ActiveXObject('Microsoft.XMLHTTP')
   } else {
     _error("Could not create XMLHttpRequest on this browser");
     return null;
   }
 },

More information:

  • http://ajaxpatterns.org/Cross-Browser_Component
  • http://ajaxify.com/run/testAjaxCaller/


Would you try using jQuery or some other library's ajax functions?


<script type="text/javascript">
function ajaxFunction()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
}
</script>

this piece of code is available in W3Schools you can learn basics here like i did. hope this helps.


Strange. All the suggestions shall work. Anyway, here is what I use and it works at least for my websites.

return window.ActiveXObject ?
    new ActiveXObject("Microsoft.XMLHTTP"): new XMLHttpRequest()
0

精彩评论

暂无评论...
验证码 换一张
取 消