ardox
ardox

Reputation: 15

Using multiple window.onload events in a separate file

I've got a problem and can't figure it out and would be glad if anyone of you could help me. So basically what I am trying to do is to put multiple window.onload events in a seperate file which starts my scripts. To get clear what I mean her is my situation:

Let's say I got these files:

index.html

    <html>
    <head>
        <link rel="stylesheet" href="style.css" type="text/css">
        <script type="text/javascript" src="kalkevent.js"></script>
        <script type="text/javascript" src="reckevent.js"></script>
        <script type="text/javascript" src="winonload.js"></script>
    </head>
    <body>
        <div id="topColumn">
            ...
        </div>
        <div id="bottomColumn">
            ...
        </div>
    </body>
    </html>

kalk.js

    function kalkInit() {

    Array.prototype.slice.call(document.forms[0].elements).forEach(function(element) {
        ...
    });
    };

reck.js

    function reckInit() {
    ...
    };

So I want to load kalkInit and reckInit on window.onload . This should be handled in a separate file. What I already tried is:

winonload.js

  function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
  };
  addLoadEvent(kalkInit);
  addLoadEvent(reckInit);

But it's not working at all. So my question is if it possible what I am trying to do or not. And if it is could someone pls help me out? :)

Upvotes: 1

Views: 1229

Answers (2)

CodeCarvings
CodeCarvings

Reputation: 324

You can consider using jQuery.. In your winonload.js you need only:

$(window).on("load", kalkInit);
$(window).on("load", reckInit);

Upvotes: 1

SirDeveloper
SirDeveloper

Reputation: 276

Maybe you have to call your onloadfunction:

 <body onload="addLoadEvent();">

Upvotes: 0

Related Questions