TSCAmerica.com
TSCAmerica.com

Reputation: 5377

Find image TAG and replace the source with a domain name

I am trying to work on a Jquery code which can find src of all <img> tags in the page and replace and append that with https://s3.amazonaws.com/abc so for example

<img src="/v/abx/templates/210/images/clear1x1.gif" width="5" height="5" alt="" />

Should be replaced with

<img src="https://s3.amazonaws.com/abc/v/abx/templates/210/images/clear1x1.gif" width="5" height="5" alt="" />

The Main issue is it should happen on page Load

I have the following Javascript code but it doesnt work

<!-- START: javascript to manipulate product photo URLs -->
<script type="text/javascript">
if(location.href.indexOf(‘/product_p/’) != -1) {
var ThisPhoto=document.getElementById('product_photo').src;
var ImgServer='https://s3.amazonaws.com/abc';
var ThisDomain=document.domain;
var NewImgSrc = ThisPhoto.replace(ThisDomain,ImgServer);
document.getElementById('product_photo').src=NewImgSrc;
}
</script>
<!-- END: javascript to manipulate product photo URLs -->

Upvotes: 0

Views: 275

Answers (1)

user1726343
user1726343

Reputation:

$('img').each(function(){
    $(this).attr('src','https://s3.amazonaws.com/abc/'+$(this).attr('src'));
});

Put that in a $(document).ready handler.

Upvotes: 4

Related Questions