Search and Replace Anchor Href using Javascript

I have a site that is under development and it was made really badly so I needed a quick way to replace all anchors on page to the relevant path.

The problem is because it was a testsite that lived away from the public facing domain, the anchors had to be changed because they were all coded in as absolute paths.

I included a JavascriptDebug file and put the following in there:

function replaceAnchorsTestSite(){
	var allAnchors = document.getElementsByTagName("a");
	var string1 = "http://***.***.***.***/testsite/";
	var string2 = "http://***.***.***.***/";
	for (var i=0; i<allAnchors.length; i++) {
		var str = allAnchors&#91;i&#93;.href;
		allAnchors&#91;i&#93;.href = str.replace(string2, string1);
	}
}
window.onload = replaceAnchorsTestSite;
&#91;/code&#93;

I then placed the following at the bottom of the index page to start it all up.

&#91;code lang="javascript"&#93;
<script src="http://***.***.***.***/testsite/jsDev.js" type="text/javascript"></script>
0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
stone
7 years ago

Hi,
thank you so much, you save my time, really 🙂

Have a nice weekend.
Stone