Solved

How do you replace a URL in an "IMG" tag with Javascript

Posted on 2014-10-27
2
166 Views
Last Modified: 2014-10-28
I moved a website and the images are no longer at the same URL, but I don't want to manually change all the pages on the website.  I'd like to write some javascript to add to the page to change all instances of the URL in the image tag, since I can easily add the code across the entire website.

Old Site IMG Tag
<img alt="" src="http://www.domain.com/entertainment/music/2007/images/04.000.jpg" width="270" align="left" border="0" id="exifviewer-img-1" exifid="-2145830154" oldsrc="http://www.domain.com/entertainment/music/2007/images/04.000.jpg">

Open in new window


Desired New Site IMG Tag
<img alt="" src="http://files.domain.com/entertainment/music/2007/images/04.000.jpg" width="270" align="left" border="0" id="exifviewer-img-1" exifid="-2145830154" oldsrc="http://files.domain.com/entertainment/music/2007/images/04.000.jpg">

Open in new window


Basically, I need to change www.domain.com to files.domain.com
0
Comment
Question by:dauyeung
2 Comments
 

Assisted Solution

by:expertexchangers
expertexchangers earned 50 total points
ID: 40407770
Hi
You can do as follows. Please check the example I created http://jsfiddle.net/913e8ryg/
<img alt="" src="http://files.domain.com/entertainment/music/2007/images/04.000.jpg" width="270" align="left" border="0" id="exifviewer-img-1" exifid="-2145830154" oldsrc="http://files.domain.com/entertainment/music/2007/images/04.000.jpg">

Open in new window


var Image = document.getElementsByTagName('img')[0].src
var newUrl = Image.replace("http://www.domain.com", "http://files.domain.com");
alert(newUrl);

Open in new window


The other way will using a texteditor and find replace all. Better is to create a global variable and assign url to it and use that variable on everyplace so that if in future you need to change url you just need to change at one place.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 450 total points
ID: 40408133
put this at the end of the page (just before </body>):
<script>
var images = document.getElementsByTagName("img");
for(var i=0;i<images.length;i++) {
     if(images[i].src.indexOf("http://www.domain.com")==0) {
        images[i].src = images[i].src.replace("http://www.domain.com", "http://files.domain.com");
     }
}
</script>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question