7

Before i ask this question, i never post about questions like this but I don't understand how to implement it in my code. i have code like this

window.onload = function() {
 var url  = getQueryVariable("url");
 document.getElementById('view').src = url;
}
window.onload = function() {
    var linkDirect = document.getElementsByClassName("frame");
    for (var i = 0; i < linkDirect.length; i++) {
        linkDirect[i].href = "http://namablog.blogspot.com/p/demo.html?url=" + linkDirect[i].href
    }
}

then, how can I make the code execution using only one window.onload

4
  • Just transform both functions in one. Or make a function which calls the other two. Commented May 9, 2013 at 14:21
  • Why do you need two onloads? Commented May 9, 2013 at 14:23
  • 1
    Learn about addEventListener - developer.mozilla.org/en-US/docs/DOM/… Commented May 9, 2013 at 14:24
  • Just transform both functions in one Commented May 9, 2013 at 14:30

4 Answers 4

23

You can use addEventListener or any jQuery equivalent.

window.addEventListener('load', function (){
    alert('Function #1');
});

window.addEventListener('load', function (){
    alert('Function #2');
});

Be sure to call these before the window is loaded.

Sign up to request clarification or add additional context in comments.

Comments

1

window.addEventListener will not work in IE so use window.attachEvent

You can do something like this

function fun1(){
    // do something
}

function fun2(){
    // do something
}


var addFunctionOnWindowLoad = function(callback){
      if(window.addEventListener){
          window.addEventListener('load',callback,false);
      }else{
          window.attachEvent('onload',callback);
      }
}

addFunctionOnWindowLoad(fun1);
addFunctionOnWindowLoad(fun2);

Comments

1

Just my 2 cents, My personal fav way of doing this is as follows:

function window_onload(cb) {
    try {
        if (typeof cb == 'function') {
            if (document.readyState == 'complete') cb();
            else if (window.hasOwnProperty('jQuery')) jQuery(window).on('load', cb);
            else if (window['addEventListener']) document.addEventListener('load', cb, false);
            else if (window['attachEvent']) {
                //  iFrame
                if (window['frameElement']) document.attachEvent('onreadystatechange', function(){ if (document.readyState === 'complete') window_onload(cb); });
                else window.attachEvent('onload', cb);
            }
            else {
                var fn = window.onload; // very old browser, copy old onload
                window.onload = function() { fn && fn(); ready(); };
            }
        }
    }
    catch (err) { if (window['console'] && console['error']) console.error("ERROR[window_onload()]", err); }
    return window;
}

This pretty much covers just about everything. What little (mainly extremely old browsers I suppose?) it doesn't cover, you could easily debug, if needed. This also goes ahead and launches the callback if the document is already 'loaded'.

Comments

0

The simplest solution that has worked for me:

function doOnLoad() {
        onloadfn1();
        onloadfn2();
        onloadfn3();
}
window.onload = doOnLoad;

This article has explained it in details: http://developer.expressionz.in/blogs/2009/03/07/calling-multiple-windows-onload-functions-in-javascript/

I hope this helps some of you.

Comments

Your Answer

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.