最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览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?

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";
};
发布评论

评论列表(0)

  1. 暂无评论