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
2 Answers
Reset to default 12Don'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