1

I haven't been working with javascript before, so I am still learning the basics.

I have included two javascript files in my main file called index.html. The reason for this is to let users select which javascript file to show (diagram 1 or diagram 2) by clicking on the navigation tool bar. The problem is that both of these javascript files have a equal method called window.onload, so they replace each other. How can I prevent these two javasript files to replace each other?

First javascript file:

window.onload = function {// Preview diagram 1}

Second javascript file:

window.onload = function {// Preview diagram 2}

Here is a snip of what I have in my index.html

<script type="text/javascript" src="js/diagram1.js"></script>
<script type="text/javascript" src="js/diagram2.js"></script>
Dler Hasan
  • 233
  • 1
  • 11
  • You can use jQuery to bind onload event to let both handlers to be fired: `$(window).on('load', function() {...});` – A. Wolff Apr 18 '15 at 11:02

1 Answers1

0

In your both diagram1.js & diagram2.js files, use:

$(window).on("load", function() {
       // your init code
});

to do stuff on window load instead of registering function to window.onload.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
  • This is so because, `window.onload` is kind of a global object, so when you are registering it in your diagram2.js file, it is getting overwritten. – Shashank Agrawal Apr 18 '15 at 15:19