3

为了更清楚,我让它变得简单(所以我们假设 iframe.attr() 将在之前的加载完全完成后被调用):

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/");

控制台日志如下所示:

I was loaded:1
I was loaded:2

当 iframe 从 stackoverflow.com 加载新内容时,它会触发回调“trackLoads”,但由于某种原因,iframe 永远不会触发 204 No Content 的回调。

iframe 更改“src”属性后如何检测“204 No Content”?

4

1 回答 1

-2

将您以前的代码更改为:

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 );
   }
};

来自:http ://api.jquery.com/load/

于 2014-10-01T10:33:46.827 回答