Chrome浏览器中的iframe onload与IE11 [英] iframe onload in chrome vs IE11

查看:244
本文介绍了Chrome浏览器中的iframe onload与IE11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Chrome编写我的第一个Web应用程序(由于内置了完善的Inspect工具),直到在IE11中进行测试之前,一切都还不错.

I've been writing my first webapp using Chrome (because of the neat built-in Inspect tool) and all was fine until I tested in IE11 .

基本上是一种形式&用于上传文件的iframe(但在代码段中,我仅着眼于通过消除所有内容来最小化行为的再现).

It's basically a form & iframe that is used for uploading a file (but in the snippet I've just focused on minimal reproduction of the behavior by cutting all that out).

该行为是在Chrome中,我单击提交",并且该表单触发了iframe onload事件并运行了一些代码.在IE11中,页面加载时会触发onload,并且单击提交"按钮时,会触发两次.

The behavior is that in Chrome I click "Submit" and the form triggers the iframe onload event and run some code. In IE11, the onload fires as the page is loaded, and when Submit button is clicked, it fires twice.

似乎我应该使用其他事件处理程序,但不确定要捕获什么.我尝试了onsubmit,但是它什么也没做.

It seems like I should be using some other event handler, but not sure what to catch. I tried onsubmit, but it does nothing.

来自html块的代码:

Code from html block:

<form enctype = "multipart/form-data" target="upload-iframe">
  <input type = "submit" />
</form>
<iframe name="upload-iframe" id="myFrame" hidden="hidden"></iframe>

<script type="text/javascript">
  var iframe = document.getElementById('myFrame');
  iframe.onload = function() {
    alert("I'm doing something");
  }
</script>

谢谢!

推荐答案

我玩了一下,最终发现将onload块包装到jQuery $(document).ready(function(){...})中;为了将其延迟到页面加载之后,它会处理页面加载时的加载事件触发问题以及双触发问题.

I played around and eventually found that by wrapping the onload block into a jQuery $(document).ready(function() {...}); to delay it until after page load, it takes care of both the issue with the load event trigger upon page load, as well as the double trigger issue.

这篇关于Chrome浏览器中的iframe onload与IE11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆