最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - append string to image src using js - Stack Overflow

matteradmin5PV0评论

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?

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?

Share Improve this question asked Jul 2, 2012 at 3:08 jeffimperialjeffimperial 251 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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 ment:

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);    

Use this:

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

comment list (0)

  1. No comments so far