0

I have attached 2 Javascript files into my html. when i attached the second, the first one stopped working. (when i detach the second, the first file is working fine)

i think the reason is that both of those javascript files contain

window.onload=function() {
// do stuff
}

is there a reason for something like that? i work it out?

the attaching on the html file look like this:

<!DOCTYPE html>
<html>
<head>
  <script src="CyclingImage.js"></script>
    <script src="blackbar.js"></script>
</head>

<body>
//stuff
</body>
LiranC
  • 2,400
  • 1
  • 27
  • 55

3 Answers3

0

Looks like you're overriding the first window.onload declaration with the second, due to the equals operator.

Why not use jQuery document ready function instead of window.onload.

$(function() {
    // your code executes when document has loaded
});
Jon Miles
  • 9,605
  • 11
  • 46
  • 66
0

you can change one of the window.onload to $(document).ready(). Or use body onload.

nsylmz
  • 277
  • 2
  • 10
0

The assignment operator of the second script to window.onload is overriding the first. Since you are not using JQuery, use the built in addEventListener to listen for the onload event in both scripts instead of setting window.onload.

function onLoadFunction(){
    //DoSomething
}
window.addEventListener("load", onLoadFunction, false);
Preston S
  • 2,751
  • 24
  • 37