Javascript – jQuery: Execute function on document.ready() and window.load()

functionjavascriptjquery

I'm trying to execute a jQuery function twice. Once when the DOM is ready, and then again when the page loads. Most of the time the second call isn't necessary, but every once in a while it is. Here is my code:

$(document).ready(function() {
    function someFunction() {
        alert('function complete');
    }
});

$(window).load(function() {
    someFunction();
});​

What am I doing wrong?

Best Answer

you are defining someFunction in the function you pass to $(document).ready()...the scope of the function should be outside it... try this:

function someFunction() {
    alert('function complete');
}

$(document).ready(someFunction);
$(window).load(someFunction);​