Solved

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

Posted on 2014-10-27
2
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

687 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