Solved

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

Posted on 2014-10-27
2
167 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

791 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