0

I've got a page where there is <img src="/images/product/whatever-image.jpg" alt="" />

I want it so that upon loading of the page, the string "?Action=thumbnail" is appended to src's value, making it src="/images/product/whatever-image.jpg?Action=thumbnail"

how do I achieve this using js?

2 Answers 2

4
window.addEventListener('load', function(){
    /* assuming only one img element */
    var image = document.getElementsByTagName('img')[0];

    image.src += '?Action=thumbnail';
}, false);

Note, changing the source of the image will "re-fetch" the image from the server — even if the image is the same. This will be better done on the server-side.


Update after comment:

window.addEventListener('load', function(){
    /* assuming only one div with class "divclassname" and img is first child */
    var image = document.getElementsByClassName('divclassname')[0].firstChild;

    image.src += '?Action=thumbnail';
}, false);    
Sign up to request clarification or add additional context in comments.

2 Comments

how would i proceed if i wanted to get element by the div class within which the image is held?
awesome man thanks! img wasn't the first child in the div, the image was wrapped in a link tag, so i gave the link a classname, and used that instead. thanks!
1

Use this:

window.onload = function() {
    document.getElementById('myImage').src += "/Action=thumbnail";
};

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.