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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码