Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks for the answers!</p> <p>I rewrote my script2.js using addEventListener and attachEvent like this:</p> <pre><code>//addOnloadHandler(initAll1); // it works only when uncommenting this addOnloadHandler(initAll2); function initAll2() { alert("initAll2"); if (linkHasOnclickHandler(document.getElementById("redirect"))) { alert("correct!"); } else { alert("wrong!"); } } function addOnloadHandler(newFunction) { if (window.addEventListener) { // W3C standard window.addEventListener('load', newFunction, false); // NB **not** 'onload' } else if (window.attachEvent) { // Microsoft window.attachEvent('onload', newFunction); } } function linkHasOnclickHandler(element) { var oldevent = document.getElementById("redirect").onclick; if (typeof oldevent == "function") { return true; } else { return false; } } </code></pre> <p>As you can see, addOnloadHandler() has been rewritten using the native implementations you guys mentioned. I left script1.js untouched. </p> <p>The resulting code still does not work (i.e., the "wrong!" alert message is shown). It only works when I register the onload initAll1() handler twice by uncommenting the first line of code in script2.js.</p> <p>Apparently, mixing </p> <pre><code>window.onload = handler1; </code></pre> <p>and</p> <pre><code>window.addEventListener('load', handler2, false); </code></pre> <p>or</p> <pre><code>window.attachEvent('onload', handler2); </code></pre> <p>does not work fine.</p> <p>Is there any way to work around this problem that does not imply touching script1.js? </p> <p>Just in case you wonder why I don't want to touch script1.js, the reason is that I want my code (script2.js) to be reusable in other projects as well, no matter which other js files each project uses. So, it should work with every possible event-handling registration method used in script1.js.</p> <p>thanks once more for your help!</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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