www.webdeveloper.com
Results 1 to 5 of 5

Thread: need to replace part of all the links in html code

  1. #1
    Join Date
    Mar 2009
    Posts
    11

    need to replace part of all the links in html code

    Hi peeps,

    I have a a large number of pages in which I want to replace parts of links within it. I will give you an example: I have pages that have the following link:

    Code:
    <a href="www.siteone.com/picLib/myPic">My Pic</a>
    I want to replace the domain www.siteone.com to www.sitetwo.com so that the a href tag to be as shown below:
    Code:
    <a href="www.sitetwo.com/picLib/myPic">My Pic</a>
    I need to replace all the links accross a large number of pages. I did use the following javascript code to change all the siteone links to sitetwo:
    PHP Code:
    function fixLinks() {
     var 
    replacementURL "http://www.sitetwo.com";  // URL to change to
     
    var lookfor "http://www.siteone.com";      // Text to look for 
     
    var links document.getElementsByTagName("a");
     for (
    i=0;i<links.length;i++) {
       if (
    links[i].href.indexOf(lookfor) > -1links[i].href replacementURL;
     }

    The html body onload tag ran the above script but it changes the whole link instead of part of the link. So the link: www.siteone.com/picLib/myPic will change to www.sitetwo.com instead of www.sitetwo.com/picLib/myPic


    Any Ideas on how to do this

    Thanks,
    Ket

  2. #2
    Join Date
    Nov 2008
    Posts
    157
    Code:
    function fixLinks() {
    	var replacementURL = "http://www.sitetwo.com";  // URL to change to
    	var lookfor = "http://www.siteone.com";      // Text to look for
    	var regexp = new RegExp("^"+lookfor+"(.*)$", "i")//Make a little regex
    	var links = document.getElementsByTagName("a");
    	for (i=0;i<links.length;i++) {
    		tmp = links[i].href;	//Get the href
    		links[i].href = tmp.replace(regexp, replacementURL+"$1"); // replace it
    	}
    }

  3. #3
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    In this case you can actually simplify it to:
    Code:
    function fixLinks() {
    	var links = document.getElementsByTagName("a");
    	var replacementURL = "www.sitetwo.com";  // URL to change to
    	var lookfor = "www.siteone.com";      // Text to look for
    	for (i=0;i<links.length;i++) {
    		links[i].href=links[i].href.replace(lookfor, replacementURL);
    	} 
    }
    54 68 65 42 65 61 72 4D 61 79

  4. #4
    Join Date
    Nov 2008
    Posts
    157
    Nvm
    Last edited by mintedjo; 03-24-2009 at 12:32 PM. Reason: Nvm...

  5. #5
    Jgy3183 is offline Web Applications Develope
    Join Date
    Mar 2009
    Posts
    29
    Though you can do this in JavaScript (though it would have to run after all your elements have loaded), I suggest you actually change the source code of the files itself. I would use a program that can globally find and replace in a folder. Dreamweaver, for example can do this.

    -J

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles