Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Modernizr to test a browser for AJAX support?

As a big fan of Progressive-Enhancement, I use Modernizr to test browser capabilities. How can I use Modernizr to test the browser's support of AJAX (specifically, jQuery's AJAX functions)?

like image 924
Jeromy French Avatar asked Sep 13 '25 20:09

Jeromy French


2 Answers

You don't need Modernizer to test this, jQuery has it built-in.

if (jQuery.support.ajax) {
    alert("Ajax is supported!");
}

Although i'm not sure exactly how jQuery handles $.ajax calls when it isn't supported. It will either fail silently, or trigger the error callback. If it's important to you, you should test it.

like image 154
Kevin B Avatar answered Sep 15 '25 21:09

Kevin B


For what it's worth, I have added issue #734 to Modernizr to add a test for AJAX, developed the test, created a JSFiddle to test, er, the test...and initiated a pull-request (issue #735) back to the fine folks who maintain Modernizr.

The relevant JS:

Modernizr.addTest('ajax', function() {
    var xhr = new XMLHttpRequest();
    return !!('onreadystatechange' in xhr);
});
like image 34
Jeromy French Avatar answered Sep 15 '25 21:09

Jeromy French