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
Post a Comment