最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论