Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding a second submit handler
    text
    copied!<p>I am trying to modify an existing web page which looks like this</p> <pre><code>&lt;script type="text/javascript" src="s1.js"&gt;&lt;/script&gt; </code></pre> <p>s1.js has something like this</p> <pre><code>window.onDomReady = DomReady; function DomReady(fn) { if(document.addEventListener) { document.addEventListener("DOMContentLoaded", fn, false); } else { document.onreadystatechange = function(){chState(fn);}; } } function chState(fn) { if(document.readyState == "interactive" || document.readyState == "complete") fn(); } window.onDomReady(addHndlrs); function addHndlrs() { var forms = document.getElementsByTagName("form"); for(var i = 0; i &lt; forms.length; i++) { var form = forms[i]; if(form.addEventListener) { form.addEventListener("submit", DoValidate, false); } else if (form.attachEvent) { form.attachEvent("onsubmit", DoValidate); } Other stuff. } </code></pre> <p>When I click Submit on the form, DoValidate does called.</p> <p>I am trying to modify this page to add another submit handler which is called after the first one.</p> <p>I copied the above code, changed function names &amp; put into s2.js.</p> <pre><code>window.onDomReady = DReady; function DReady(fn) { if(document.addEventListener) { document.addEventListener("DOMContentLoaded", fn, false); } else { document.onreadystatechange = function(){Chk1State(fn);}; } } function Chk1State(fn) { if(document.readyState == "interactive" || document.readyState == "complete") fn(); } window.onDomReady(myready); function myready() { var forms = document.getElementsByTagName("form"); for(var i = 0; i &lt; forms.length; i++) { var form = forms[i]; if(form.addEventListener) { form.addEventListener("submit", mynewhandler, false); } else if (form.attachEvent) { form.attachEvent("onsubmit", mynewhandler); } } } </code></pre> <p>In the form html, I added a reference to it</p> <pre><code>&lt;script type="text/javascript" src="s1.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="s2.js"&gt;&lt;/script&gt; </code></pre> <p>My new submit handler never gets called. I debugged it through firebug - I see DReady being called and my DOM Ready handler being registered. However myready never gets called, so my submit handler never gets registered. </p> <p>What am I missing here? I tried changing order of inclusion of s1.js and s2.js but that doesn't seem to help. I want to do this without modifying s1.js</p>
 

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