Member Avatar
ziyaddinsadigov

I want to handle 404 error if script can't load some resouce after document is ready.
Here is code:

(function( $ ){

  $.fn.onair=function(){

    var tid = setInterval( function () {
    if ( document.readyState !== 'complete' ){
      return;
      alert("salam")
    }
      else if(document.readyState=='complete'){
          if (document.onerror){
        //ajax
         $.ajax({
  statusCode: {
    404: function() {
      alert("page not found");
    }
  }
});
         //ajax
         alert("s");
         clearInterval( tid );
         }
    }
}, 10 );

  }

})( jQuery )

Try something similar to the script below... The status is whatever number you want to handle...

success: function(data, textStatus, XMLHttpRequest){
    console.log('Error: ' + textStatus);
},
error:function (xhr, ajaxOptions, thrownError){
    alert(xhr.status);
    alert(xhr.statusText);
    alert(xhr.responseText);
}
Member Avatar
ziyaddinsadigov

ok, thanks! If i want to do some function after the document loading completed and i want to do this after testing if document has error?

Yes, you could try to check what the response status from your ajax to see if you get an error back.