An alternative way to automatically submit a form if necessary without using global variables:
document.getElementById('yourFrm').addEventListener('submit',(function() { 'use strict'; var imgLoaded,img,tmpImgLoad; imgLoaded = false; img = document.getElementById('yourImg'); tmpImgLoad = function () { imgLoaded = true; img.removeEventListener('load',tmpImgLoad,false);//remove obsolete listener? }; img.addEventListener('load',tmpImgLoad,false); return function(e) { if (!imgLoaded) { tmpImgLoad = function() { imgLoaded = true; img.removeEventListener('load',tmpImgLoad,false); document.getElementById('yourForm').submit(); }; e.preventDefault(); e.stopPropagation(); } }; })(),false);
This code associates a load event listener with your image element, which sets the close variable to true. If this variable is false when submitting the form, the callback for the upload event changes to submit the form.
I admit that this is a little more complicated, but only because you know: globals are not the only way to solve this problem.
Just walked through JSLint. A couple of tiny tweaks to both “wrap messy white space” and “paragraph browser” this code goes without any warnings or errors at all
source share