【问题标题】:How to rightly detect iframe was loaded with response 204 No Content?如何正确检测 iframe 是否加载了响应 204 No Content?
【发布时间】:2014-09-16 13:11:50
【问题描述】:

为了更清楚,我把它简单化了(所以我们假设 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”?

【问题讨论】:

  • 您无法直接检测到 204,但您可以使用 setTimeout 来确定内容可能不会加载。我会更担心您的服务器为错误的 url 返回错误的代码。

标签: javascript jquery html iframe http-status-code-204


【解决方案1】:

把你之前的代码改成这样:

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/

【讨论】:

  • 这与 iFrame 加载事件无关! jquery.load 完全不同。
猜你喜欢
  • 2012-08-29
  • 2018-08-05
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2012-09-30
  • 2019-09-05
  • 2010-10-29
  • 1970-01-01
相关资源
最近更新 更多