Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding multiple onload handlers
    primarykey
    data
    text
    <p>I have two js files, each one with its own window.onload handler. Depending on how I attach the two onload handlers to the window object I get a different behaviour on the second handler. </p> <p>More specifically, here is my html file:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Welcome to our site&lt;/title&gt; &lt;script type="text/javascript" src="script1.js"&gt; &lt;/script&gt; &lt;script type="text/javascript" src="script2.js"&gt; &lt;/script&gt; &lt;/head&gt; &lt;body id="pageBody"&gt; &lt;h2 align="center"&gt; &lt;a href="http://www.whatever.com" id="redirect"&gt; Wellcome to our site... c'mon in! &lt;/a&gt; &lt;/h2&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It loads two js files, script1.js and script2.js.</p> <p>Here is the version of these two scripts that leads to the (at least by me) unexpected behaviour.</p> <p>Script1.js:</p> <pre><code>window.onload = initAll1(); // attach first onload handler function initAll1() { alert("initAll1"); document.getElementById("redirect").onclick = foo; // attach an onclick handler } function foo() { alert("we are in foo"); return false; } </code></pre> <p>Script2.js:</p> <pre><code>addOnloadHandler(initAll2); // with this we should attach a second onload handler function initAll2() { alert("initAll2"); if (linkHasOnclickHandler(document.getElementById("redirect"))) { alert("correct!"); } else { alert("wrong!"); } } function addOnloadHandler (newFunction) { var oldevent = window.onload; if (typeof oldevent == "function") { window.onload = function() { if (oldevent) { oldevent(); } newFunction(); }; } else { window.onload = newFunction; } } function linkHasOnclickHandler() { var oldevent = document.getElementById("redirect").onclick; if (typeof oldevent == "function") { return true; } else { return false; } } </code></pre> <p>In Script2.js I tried to add the second onload handler in a nice noninvasive way using function addOnloadHandler(). This function does not make any assumption on whether there is already any onload handler attached to the window object. It is noninvasive because it should add the new handler without deleting previous ones.</p> <p>The thing is that when loaded with addOnloadHandler(), initAll2() is not capable of detecting the fact that document.getElementById("redirect") already has foo() attached as an onclick event handler (see initAll1()). The alert message "wrong!" is triggered, which to me seems to be the wrong behaviour.</p> <p>When I forget about addOnloadHandler() and attach both onload handlers in Script1.js using:</p> <pre><code>window.onload = function () {initAll1(); initAll2();}; </code></pre> <p>then everything works as expected, and initAll2() launches the "correct!" alert message.</p> <p>Is there something wrong about addOnloadHandler()? Could anybody make it work? I would really like to use it instead of the second method.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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