2

I have two files file1 and file2 and am using window.onload in both files. when am linking to these files in html

<script type="text/javascript" src="file1.js">
<script type="text/javascript" src="file2.js">

the second file is running (its onload event is triggered) while the first file is dead. what can I do to make both files run? Is there any other way than making a huge file (file1's content + file2's content)?

asked May 28, 2013 at 16:31

1 Answer 1

10

Use addEventListener :

window.addEventListener('load', function(){

Callbacks are all called, adding one doesn't remove the ones you added before.

If you want to be compatible with IE8, use a shim like this :

function addOnLoad(callback) {
 if (window.addEventListener) window.addEventListener('load', callback)
 else (window.attachEvent('onload', callback);
}
addOnLoad(function(){
answered May 28, 2013 at 16:32
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.