简体   繁体   中英

How to check browser support ajax or not?

I am using jQuery 1.12 . But my question is how to check browser support ajax or not. If browser support ajax then i want to change page content using ajax.

Almost every browser now support AJAX.

If you still want to test it you can check for XMLHttpRequest

if (window.XMLHttpRequest) {

     // Supports Ajax.

    } else {

     //No. 
}
<script language="javascript" type="text/javascript">
   <!-- 

   function ajaxFunction(){
      var ajaxRequest;  // The variable that makes Ajax possible!

      try{
         // Opera 8.0+, Firefox, Safari (1st attempt)
         ajaxRequest = new XMLHttpRequest();
      }catch (e){
         // IE browser (2nd attempt)
         try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
         }catch (e) {
            try{
         // 3rd attempt
               ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){

               alert("Failure");
               return false;
            }
         }
      }
   }
   //-->
   </script>

Try individually three times to make the XMLHttpRequest object. If all cases fail, it is sure that the browser is outdated and doesn't support ajax. Hope this helps!

Found this at: http://programmerguru.com/ajax-tutorial/browser-support/

Here is the code snippet which checks if the browser supports AJAX or not.
?
<script type="text/javascript">
var xmlhttp;
function checkAJAXSupport() {
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
      xmlhttp= new XMLHttpRequest();
      alert("Yes. Your browser must be one among them - Mozilla, Safari, Chrome, Rockmelt, IE 8.0 or above");
    } else if (window.ActiveXObject) { // IE
      try {
        xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
        alert("Yes. Your browser must be IE");
      } 
      catch (e) {
        try {
          xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
          alert("Yes. Your browser must be IE");
        } 
        catch (e) {}
      }
    }
 if (!xmlhttp) {
      alert("No. Giving up Cannot create an XMLHTTP instance. Your browser is outdated!");
      return false;
    }
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM