Search code examples
javascripthtmlfunctionimagesrc

Setting img tag's src as the result of a javascript function


I have a function getImgurUrl() and I want what it returns to be the src value of a img tag.

How can I do this?

my function:

function getImgurUrl() {
    //get imgur url for current image
    var cookieArray = document.cookie.split(";");
    var encodedURL = cookieArray[2].split("=");
    var decodedURL = decodeURIComponent(encodedURL[1]);
    return decodedURL;
}

and img tag:

<img id="image" name="image" src=""  alt="If you're seeing this something is wrong.">

Solution

  • window.onload = function() {
        document.getElementById("image").src = getImgurUrl();
    };