Note that there are some explanatory texts on larger screens.

plurals
  1. POCode not executing in img.onload
    text
    copied!<p><code>alert('loaded');</code> is never executed in the first code snippet.<br/> The only difference between the 2 code snippets is <code>alert('setting src..');</code> is commented out in first example, but left in the 2nd example.<br/> Why is <code>alert('loaded');</code> not executed in first example?</p> <p>I have a html file, containing a form, when the form is submitted <code>Utils.imgExists()</code> is called.<br/> <code>Utils.imgExists()</code> is declared in a JS file, included in the html document.</p> <pre><code> &lt;form action="anotherPage.html" method="get" onsubmit="return Utils.imgExists('http://www.nasa.gov/images/content/711375main_grail20121205_4x3_946-710.jpg')"&gt; &lt;div id="imageInput"&gt; &lt;input type="text" id="image" name="image" value="http://www.nasa.gov/images/content/711375main_grail20121205_4x3_946-710.jpg" title="Paste an image URL e.g http://server.com/path/to/image.jpg"/&gt; &lt;/div&gt; &lt;button type="submit"&gt;Submit&lt;/button&gt; &lt;/form&gt; </code></pre> <p><code>Utils.imgExists()</code> inside Utils.js:</p> <pre><code>var Utils = { imgExists: function(url){ function imgExists(url, callback) { alert('in imgExists'); var img = new Image(); img.onerror = function() { alert('error'); callback(false); }; img.onload = function () { alert('loaded'); callback(true); }; img.src = url; //alert('setting src..'); } function checkImage(exists) { alert("Image exists: " + exists); } imgExists('http://www.nasa.gov/images/content/711375main_grail20121205_4x3_946-710.jpg', checkImage); } }; </code></pre> <p>This is the second example:</p> <pre><code>var Utils = { imgExists: function(url){ function imgExists(url, callback) { alert('in imgExists'); var img = new Image(); img.onerror = function() { alert('error'); callback(false); }; img.onload = function () { alert('loaded'); callback(true); }; img.src = url; alert('setting src..'); } function checkImage(exists) { alert("Image exists: " + exists); } imgExists('http://www.nasa.gov/images/content/711375main_grail20121205_4x3_946-710.jpg', checkImage); } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload