onload事件不起作用


onload event not working

有很多关于这个的帖子。我经历了所有这些,但没有运气。我挣扎了这么久(Onload事件不起作用)。我找不到任何解决办法。

我试过了。。1.

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script type="text/javascript">
document.getElementById('pdfcontent').onload = function() {
   alert("working");
}
</script>
</head>
<body >
<div id="loadingpdf">Loading pdf</div>
<iframe src="http://prodevapp.com/ArmyPublicRelation/pdf/royalthaiarmynews/news_20141103173559.pdf" id="pdfcontent" />
</body>
</html>

2.

$.ajaxSetup(
            {
                cache: false,
                beforeSend: function() {
                    $('object').hide();
                    $('#loadingpdf').show();
                },
                complete: function() {
                    $('#loadingpdf').hide();
                    $('object').show();
                },
                success: function() {
                    $('#loadingpdf').hide();
                    $('object').show();
                }
            });
            var $container = $('object');
            $container.load(function(){
        alert("Image loaded.");});

3.

         <body >
    <script>
function my_code(){
alert(" working");
}
var iframe = document.createElement("iframe");    
iframe.src = "http://prodevapp.com/ArmyPublicRelation/pdf/royalthaiarmynews/news_20141103173559.pdf";
document.body.appendChild(iframe);
iframe.onload=my_code;
</script>
</body>

4.

$("iframe").on('load', function() {
  alert('Image Loaded'); 
}).each(function() {
  if(this.complete) $(this).load();
});  //this worked one time but later it doesn't work it seems some cache problem. 

如何解决此问题?

您可以在这里找到帮助

有趣的是,这个帖子上选择的答案表明你无法跟踪pdf加载。

  1. 重复的getElementById()返回null,即使该元素存在,并且您也缺少<iframe>的结束标记
  2. 您使用的是iframe,而不是Ajax,因此监视Ajax不会起作用
  3. 在为iframe变量赋值之前,请尝试使用该变量。
    • 编辑后,您可以尝试在定义函数之前使用它。当函数被提升时,它们不会从以后的脚本中被提升
    • 再次编辑后,它就工作了
  4. 此外,重复的getElementById()返回null,即使该元素存在