问题
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?
回答1:
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/
来源:https://stackoverflow.com/questions/25869737/how-to-rightly-detect-iframe-was-loaded-with-response-204-no-content