当url生成pdf输出时,iframe onload无法正常工作

him*_*yan 5 html javascript iframe jquery

我有一个生成PDF文件的URL.为了生成PDF,我使用iframe来加载该URL并在用户的同一页面中生成PDF文件.

当用户点击按钮时,它将显示loader在屏幕上.并且当iframe加载完成时应该隐藏(当PDF下载弹出时).我使用下面的代码,但它不起作用,loader在下载弹出窗口打开后仍然显示(如果我更改url与任何其他像google.com然后它将工作).

这是我的代码片段.

$("#test").click(function(){
  iframe = document.getElementById("download-container1");
  if (iframe === null)
  {
    $("#ajax_loading").show();
    var url="http://test.coachfxlive.com/ajax/ajax-action.php?type=s&vid=0&playlist_id=11&perpage=1&action=get_profile";
    iframe = document.createElement('iframe');  
    iframe.id = "download-container1";
    document.body.appendChild(iframe);

    $('#download-container1').load(function () {
      $("#ajax_loading").hide();
    });
    iframe.src=url;
  }
  
});
Run Code Online (Sandbox Code Playgroud)
.ajax_loading {
    background-color: rgba(0, 0, 0, 0.7);
    display: none;
    height: 100%;
    left: 0;
    position: fixed;
    top: 0;
    width: 100%;
    z-index: 99999;
}
Run Code Online (Sandbox Code Playgroud)
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </head>
  <body>
    <div class="ajax_loading" id="ajax_loading"><span>Please Wait... Loading</span></div>
    <button id="test">click</button>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

iframe完整的负载,需要隐藏的div ajax_loading.我在这做错什么吗?还有其他解决方案吗?

提前致谢.

anm*_*rti 0

您可以使用readystateiframe 属性来检查 iframe 是否已完全加载。但是,如果内容是二进制文件,则这在 Chrome 中不起作用。

设置 iframe url 后,它会每秒检查 iframe 就绪状态属性:

$("#test").click(function(){
 iframe = document.getElementById("download-container1");
 if (iframe === null)
 {
  $("#ajax_loading").show();
  var url="http://test.coachfxlive.com/ajax/ajax-action.php?type=s&vid=0&playlist_id=11&perpage=1&action=get_profile";
  iframe = document.createElement('iframe');  
  iframe.id = "download-container1";
  document.body.appendChild(iframe);

  //$('#download-container1').load(function () {
  //     $("#ajax_loading").hide();
  //});
  iframe.src=url;
  checkFinishedDownload(iframe);
}

 });

function checkFinishedDownload(ifr) {
     if (ifr.readyState == "complete" || ifr.readyState == "interactive") {
         // Here hide the spinner
         $("#ajax_loading").hide();
    }
     else {
        setTimeout(function () {
            checkFinishedDownload(ifr);
        }, 1000);
     }
}
 }
Run Code Online (Sandbox Code Playgroud)

检查我的问题:Iframe.readyState does not work in chrome

编辑: 在 Firefox 中您可以使用该onload事件:

function checkFinishedDownload(ifr) {
     if ($.browser.mozilla) {
         ifr.onload = function () {
            $("#ajax_loading").hide();
          }

     }else{
          iframeCheck(ifr);
     }
}

function iframeCheck(iframe){
       if (iframe.readyState == "complete" || iframe.readyState == "interactive") {
         // Here hide the spinner
         $("#ajax_loading").hide();
    }
     else {
        setTimeout(function () {
            checkFinishedDownload(iframe);
        }, 1000);
     }
}
Run Code Online (Sandbox Code Playgroud)