0

I am a bit confused here. I thought that the function specified in window.onload did not execute before the page was loaded. Nervertheless, I get an error in the below code (heres the jsfiddle version):

<script>
    function updateImage(url){     
        document.getElementById("foo").src = url;
    }    
    window.onload = updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");
</script>

<img id="foo" alt="" src="http://dummyimage.com/100x100/000/fff.png&text=bar" />

It gives me:

Error: document.getElementById("foo") is null

When moving the image above the script all works well.

1
  • you could call it after the id, or use a dom ready function Commented Oct 11, 2011 at 7:50

3 Answers 3

6

window.onload expects to be a function - which it will call when the page is loaded. But what you've assigned to it is not a function. You assigned

updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");

which immediately executes the updateImage function, and since you don't have a return statement in the body of updateImage, the return value is undefined. Therefore, at the end, window.onload has the value undefined.

A solution would be to change that bit of code to:

window.onload = function(){
    updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");
}

This will cause it to call the function when the window has been loaded and do what you'd expect.

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

Comments

0

You can use trigger that will check is element loaded.

<script>

    function updateImage(url){     
        document.getElementById("foo").src = url;
    }

    function initOnLoad(sElementName) {
        var oElement = (sElementName == "body") ? document[sElementName] : document.getElementById(sElementName); 
        if(oElement != null && typeof(oElement) != "undefined") { 
            updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");
        } 
        else { 
            setTimeout(function() { 
                initOnLoad(sElementName); 
            }, 0); 
        } 
    }

    initOnLoad('body');
</script>

<img id="foo" alt="" src="http://dummyimage.com/100x100/000/fff.png&text=bar" />

Comments

0

You have an error in your code.

window.onload = updateImage("...");

You are not assigning a function handler to the window onload. You are assigning the RESULT of the updateImage function. Which is executed immediately before image is loaded or even added to the DOM. To assign function handler you need:

window.onload = updateImage;

or

window.onload = function(){
  updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux");
}

or by using jQuery

$(document).ready(function(){
  updateImage("http://dummyimage.com/100x100/000/fff.png&text=qux"); 
});

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.