weixin_33725126 2015-02-18 11:09 采纳率: 0%
浏览 393

onload事件不起作用

There are so many posts for this. I have gone through all of those, yet no luck. I'm struggling for so long (Onload event is not working ). I dint find any solution.

I have tried.. 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. 

How to fix this?

  • 写回答

2条回答 默认 最新

  • weixin_33725722 2015-02-18 11:21
    关注
    1. Duplicate of getElementById() returns null even though the element exists and you are also missing the end tag for the <iframe>.
    2. You are using an iframe, not Ajax, so watching for Ajax things won't work
    3. You try to use the iframe variable before you assign a value to it.
      • After editing it - you try to use a function before it has been defined. While functions are hoisted, they aren't hoisted from later scripts.
      • After editing it again - it works.
    4. Also a duplicate of getElementById() returns null even though the element exists
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?