Link to home
Start Free TrialLog in
Avatar of jpve
jpve

asked on

mass string() replace in an included file

Hello:

I've got a supposed Javascript question which I assumed is easy (and still do), but I apparently can't get it to work.

I'm developing a site, which also includes an SHTML header.  In this header is a reference to the name of the company...normal text.  This header is called on each page throughout the site, but they have recently switched their company name, so I kinda want to take care of that via JavaScript - something that will search the entire body of both the header and the parent file, look for a string containing the text of the old name of the company, and replace it before anyone even sees anything.  Obviously this would have to be done onLoad or something like that, I'm no JavaScript expert, but this is the gist of what I want to do:

Write a script that that will search the entire body of both the header and the parent file, replacing the old string(s) with the new one, and that's it.  I figured this would be easy (I could do it in PHP in a second), but the client has created a bunch of stuff before I got there and had it done in JavaScript/HTML so.

Any assistance which could be offered would be greatly appreciated.
Avatar of hackman_3vilGuy
hackman_3vilGuy

<SCRIPT>
//Place at end of body
//Script by hackman_3vilGuy
OLDNAME = "Company A";
NEWNAME = "Company B";
document.body.innerHTML = document.body.innerHTML.replace(OLDNAME,NEWNAME);
</SCRIPT>
ASKER CERTIFIED SOLUTION
Avatar of hackman_3vilGuy
hackman_3vilGuy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you need to declare a regexp in order to do this. A normal replace() will only replace the first occurence.

Example where you put the words to replace in an array:

<html>
<head>
      <script type="text/javascript">

      function replaceContent(){

            var replaceArray = ['some','the'];
            var replaceWithArray = ['else','this'];
            var theHTML = document.body.innerHTML;
            for(var no=0;no<replaceArray.length;no++){
                  var regExp = new RegExp(replaceArray[no],"gi");      // Replace "gi" with "g" if you don't want case insensitive repalce
                  theHTML = theHTML.replace(regExp,replaceWithArray[no]);
                  
            }
            document.body.innerHTML = theHTML;


      }
      
      
      window.onload = replaceContent;
      </script>
</head>
<body>
This is my text which contains some text. We want to replace the word "some" with "else"
</body>
</html>
<SCRIPT>
//Place at end of body
//Script by hackman_3vilGuy
var OLDNAME = "Company A";
var NEWNAME = "Company B";
while(document.body.innerHTML.indexOf(OLDNAME)>-1)
{
document.body.innerHTML = document.body.innerHTML.replace(OLDNAME,NEWNAME);
}
</SCRIPT>
Why use a time consuming while-loop when everything could be done in one single replace() operation?

Batalf