Solved

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

Posted on 2014-10-27
2
162 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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

744 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

18 Experts available now in Live!

Get 1:1 Help Now