What practice is recommended to check if jQuery’s Ajax function is present and is recognized by the browser, does anyone know?

Asked

Viewed 64 times

-1

What practice is recommended to check if jQuery’s Ajax function is present and is recognized by the browser, can anyone say?

// forma 1
if (!$.ajax) {
  alert ('Status xhr: não suporta Ajax');
 return false;
}
// forma 2
if (typeof $.ajax !== 'function') {
  alert ('Status xhr: não suporta Ajax');
 return false;
}

In many of the examples on the Mozilla MDN website the condition for testing functions according to the form 1 and well used, at a glance:

https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

  • please give details to those who negatively!

4 answers

2


Utilize jQuery.support.ajax or $.support.ajax

if (!$.support.ajax) {
    alert("Status xhr: não suporta Ajax");
}
  • Great Paul, thanks, I didn’t know the support function yet, besides that the tip of the Official site on the function page is also very relevant in using the Modernizr library instead, but how much the project was modeled like this, will be of great help. http://api.jquery.com/jQuery.support/

  • This function has not yet been disabled in jQuery, so while using a compatible jQuery version, there will be no need to use Modernizr.

  • According to the documentation, it was marked as obsolete in 1.9 and intended for internal use by the library. I wouldn’t recommend using it, although it’s an elegant syntax.

0

First you can create a variable to make a request ajax any

Dai inside the function you will test the possible browsers , if not accept returns an error

    function ajaxFunction() {
      var xmlHttp;

      try {
       // Firefox, Opera 8.0+, Safari
       xmlHttp=new XMLHttpRequest();
      }
      catch (e) {
        // Internet Explorer
        try  {

           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

        }
        catch (e) {
          try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e) {
             alert("Your browser does not support AJAX!");

             return false;
         }

    }
  • I didn’t find a very simple way to get this check done, this was the most quiet I found

0

I got the code on http://programmerguru.com/ajax-tutorial/browser-support/

    <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>
  • Marlon, thanks for the help, but this code I’ve already used and what I’m looking for is to simply confirm if the browser recognizes as the function of jQuery in the browser.

0

You can use When in your request

    $.when( $.ajax ).then(function( data, textStatus, 
      jqXHR ) {
      alert( jqXHR.status ); // Alerts 200
    })
    .catch(function() {
      alert ('Status xhr: não suporta Ajax');
    })
  • Thanks for the support friend, but what I seek is just to check if there is the function and if the browser recognizes to be used in the future, this in the simplest way possible.

  • I misunderstood the question, pardon , if I find some way to make it possible I make another answer

  • I edited the question because of your remark.

  • I wrote a new answer , changing the model a little bit of how you test ajax but it can work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.