Member Avatar for 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 for 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.