Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Elegant Way to Include Query String When it exists? 4 43
Randomize in Owl Carousel v1.3.2 6 21
javascript date picker in php while loop 5 28
jQuery Validate 4 22
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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now