iframe onload in chrome vs IE11

ε祈祈猫儿з 提交于 2020-01-06 08:36:15

问题


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 .

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).

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.

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

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>

Thanks!


回答1:


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.



来源:https://stackoverflow.com/questions/55476771/iframe-onload-in-chrome-vs-ie11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!