javascript - Check browsers capability of XHR and progress-event -


following question:

jquery ajax upload progress large text fields

how can make compatible older browsers?

as can see in question above, relying on xhr , progress-event. older browsers, need detect if not capable of 1 of these can skip progress-bar , still make ajax-post.

i thought work this:

$.ajax({         xhr: function() {             var xhr = $.ajaxsettings.xhr();             if (xhr instanceof window.xmlhttprequest) {                 xhr.addeventlistener('progress', function(event) {                     if (event.lengthcomputable) {                         progresspercent = math.round(event.loaded/event.total*100)+'%';                         $loader.width(progresspercent);                     }                 }, false);             }else{                 alert('xhr no instance of window.xmlhttprequest');             }             return xhr;         },         type: "post", ... 

but don't know if that's save or if there's else i'd have check.

thank you!

for close total safety, use try/catch/finally structure :

$.ajax({     xhr: function() {         var xhr = $.ajaxsettings.xhr();         try {             xhr.addeventlistener('progress', function(event) {                 if (event.lengthcomputable) {                     $loader.width(math.round(event.loaded / event.total * 100) + '%');                 }             }, false);         }         catch(err) {             //progess not available             //take appropriate action - eg hide progress thermometer             $loader.hide();         }         {             return xhr;         }     },     type: "post",     ... }): 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -