问题
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