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[i].href;
                allAnchors[i].href = str.replace(string2, string1);
        }
}
window.onload = replaceAnchorsTestSite;

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

<script src="http://***.***.***.***/testsite/jsDev.js" type="text/javascript"></script>

Share This Article

Related Posts

Comments

There are no comments on this entry.

Trackbacks

There are no trackbacks on this entry.

Add a Comment

Required

Required

Optional

Latest Domains

1. weebly.com
(1 day ago)
2. whpromotions.co.uk
(1 day ago)
3. adsl24.co.uk
(1 day ago)
4. youryoungmodels.net
(1 day ago)
5. enom.com
(1 day ago)
6. dynect.net
(1 day ago)
7. websitewelcome.com
(1 day ago)
8. hostgator.com
(1 day ago)
9. uploading.com
(2 days ago)
10. soccernetghana.com
(2 days ago)

Services

PHP MySQL Apache Flash Air Java C++ Python