Member Avatar

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

(function( $ ){


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


})( jQuery )

Recommended Answers

All 3 Replies

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){
Member Avatar

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.