2013-05-19 1 views
1

:확인 브라우저 기능과이 질문에 따라 진행 이벤트

jQuery AJAX upload progress for large text fields

이 어떻게 이전 버전의 브라우저에 호환 할 수 있습니까?

위 질문에서 알 수 있듯이 나는 XHR과 진행 상황에 의존하고 있습니다. 이제 구식 브라우저의 경우, 이들 중 하나를 사용할 수 없는지 감지해야하므로 진행 막대를 건너 뛰고 여전히 AJAX 게시를 할 수 있습니다.

$.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 is no instance of window.XMLHttpRequest'); 
      } 
      return xhr; 
     }, 
     type: "POST", 
... 

하지만 저장 만약 내가 모르거나 아무것도 만약 거기에 확인해야 할 것 :

가 나는 같이 일할 수있는 생각했다.

감사합니다. 총 안전에 가까운 무언가를

답변

2

, 당신이 시도/잡기/마침내 구조 사용할 수 있습니다

$.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 the progress thermometer 
      $loader.hide(); 
     } 
     finally { 
      return xhr; 
     } 
    }, 
    type: "POST", 
    ... 
}): 
+0

가 감사를! 그 중 하나를 시도해 보겠습니다. 내 "문제"는 모든 브라우저가 현대적이고 xhr 및 진행이 가능하므로 구식 브라우저를 사용하는 날에만 조사 할 수 있습니다.) –