3

For more clarity I made it simple (so let's presume that iframe.attr() will be called after previous load was fully completed):

var iframe = $("iframe");
var counter = 0;
var trackLoads = function(){
   console.log("I was loaded:" + counter);
   counter += 1;
};

iframe.load(trackLoads);

iframe.attr("src","http://stackoverflow.com/");
iframe.attr("src","http://localhost:9081/mobile/api/content/badPath"); //returns 204 No Content
iframe.attr("src","http://stackoverflow.com/");

Console log looks like this:

I was loaded:1
I was loaded:2

When the iframe loaded new content from stackoverflow.com, It trigger the callback "trackLoads", but iframe will never trigger callback for 204 No Content from some reason.

How to detect "204 No Content" after iframe has changed "src" attribute?

martin jrk
  • 185
  • 10
  • You can't detect 204 directly, but you could use `setTimeout` to decide that the content probably won't load. I'd be more concerned with the fact that your server is returning the wrong code for a bad url. – Ouroborus Nov 15 '20 at 07:54

1 Answers1

-3

Change your previous code to this:

var trackLoads = function(response, status, xhr){
   console.log("I was loaded:" + counter);
   counter += 1;
   if ( status == "error" && xhr.status == 204) {
      console.log( "Sorry but there was an error: " + xhr.status + " " + xhr.statusText );
   }
};

From: http://api.jquery.com/load/

Idham Perdameian
  • 2,199
  • 24
  • 34