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.
jpveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hackman_3vilGuyCommented:
<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>
hackman_3vilGuyCommented:
SORRY: more correct with var:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BatalfCommented:
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>
hackman_3vilGuyCommented:
<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>
BatalfCommented:
Why use a time consuming while-loop when everything could be done in one single replace() operation?

Batalf
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.