最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - 2 window.onload in 2 separate .js files, only 1 function loads - Stack Overflow

matteradmin4PV0评论

I have 2 external JavaScript files which both call functions via window.onload. The files conflict, so only the second js file is working.

How would I fix the window.onload so both functions work? Should I do this in the HTML file or can I fix it in one of the js files?

The HTML header is:

<link type="text/css" rel="stylesheet" href="house.css" />
<style type="text/css"></style>
<script type="text/javascript" src="slideshow_library.js"></script>
<script type="text/javascript" src="slideshow.js"></script>
<script type="text/javascript" src="house.js"></script>

house.js:

    var $ = function(id){
    return document.getElementById(id);
}

var calculateClick = function() {
    console.log("click, calculate click is running");
    //blahblahblah, lots of code
}

window.onload = function(rental){
    console.log("onload, javascript is running");
    console.log(rental);

    $("calculate").onclick = calculateClick;
}

Second window.onload is in 2 js files:

slideshow.js:

var slideShow;

window.onload = function (slideShow) {
    var params = {
        //blahblahblah lots of code
    }
}

slideshow_library.js:

var $ = function(id) { 
    return document.getElementById(id); 
}

var SlideShow = function( params ){
    //blahblah blah even more code
} 
SlideShow.prototype.displayImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.displayNextImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.displayPreviousImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.togglePlay = function(){
    //blahblah blah even more code 
}

I have 2 external JavaScript files which both call functions via window.onload. The files conflict, so only the second js file is working.

How would I fix the window.onload so both functions work? Should I do this in the HTML file or can I fix it in one of the js files?

The HTML header is:

<link type="text/css" rel="stylesheet" href="house.css" />
<style type="text/css"></style>
<script type="text/javascript" src="slideshow_library.js"></script>
<script type="text/javascript" src="slideshow.js"></script>
<script type="text/javascript" src="house.js"></script>

house.js:

    var $ = function(id){
    return document.getElementById(id);
}

var calculateClick = function() {
    console.log("click, calculate click is running");
    //blahblahblah, lots of code
}

window.onload = function(rental){
    console.log("onload, javascript is running");
    console.log(rental);

    $("calculate").onclick = calculateClick;
}

Second window.onload is in 2 js files:

slideshow.js:

var slideShow;

window.onload = function (slideShow) {
    var params = {
        //blahblahblah lots of code
    }
}

slideshow_library.js:

var $ = function(id) { 
    return document.getElementById(id); 
}

var SlideShow = function( params ){
    //blahblah blah even more code
} 
SlideShow.prototype.displayImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.displayNextImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.displayPreviousImage = function(){
    //blahblah blah even more code
}
SlideShow.prototype.togglePlay = function(){
    //blahblah blah even more code 
}
Share Improve this question edited Jul 20, 2014 at 3:35 javajenna asked Jul 20, 2014 at 2:41 javajennajavajenna 311 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Don't use the old window.onload event model. That is not very flexible — as you've noticed — and not necessary for browser support any more.

jQuery makes this easy; change window.onload = function to $(function) in each JS file.

Pure JS solution: change that to window.addEventListener('load', function).

If you are allowed/familiar to use JQuery, it would have been done like:

$(window).on('load', function() {
    //<-- do the rest here..
});

If you are still looking for pure js, try this example, its not on window.onload instead it uses document.readyState:

first.js

(function(window, document) {
    var t = setInterval(function() {
        if('plete' === document.readyState) {
            clearInterval(t);
            alert('first js loaded ...');
        }
    }, 10);
})(window, document);

last.js

(function(window, document) {
    var t = setInterval(function() {
        if('plete' === document.readyState) {
            clearInterval(t);
            alert('last js loaded ...');
        }
    }, 10);
})(window, document);

Find the git repo here

Post a comment

comment list (0)

  1. No comments so far