1

I have a hard coded webpage that I can add parts containing scripts. I need to add a script to change the word Attendees to Count throughout the page and the pages with in it. This is the code wrote and have been playing with but its not functioning.

<p> </p>
<script type="text/javascript">// <![CDATA[
document.body.innerHTML = document.body.innerHTML.replace(new RegExp("Attendees", "g"), "Count");
// ]]></script>
Pointy
415k62 gold badges600 silver badges633 bronze badges
asked Dec 17, 2013 at 16:09
5
  • take a look at the js replace documentation, you are doing it wrong: w3schools.com/jsref/jsref_replace.asp Commented Dec 17, 2013 at 16:12
  • 2
    Your script tag is in inside the body. It could well be executing before the body has been rendered and therefore the regex won't find anything. Commented Dec 17, 2013 at 16:13
  • It seems to work fine for me: jsfiddle.net/j4mUH. Please provide more information. Could it be that some content is loaded via Ajax? Commented Dec 17, 2013 at 16:15
  • As @DanIveson pointed out, if this is in the body, then it will run as soon as it's parsed, and the elements might not be loaded yet. If you place it at the end, just before </body>, then it might work. I don't see any issues with the actual JS (aside cleaning up the regex declaration by changing it to /Attendees/g) Commented Dec 17, 2013 at 16:15
  • w3schools is not a good resource for referencing documentation. MDN (Mozilla Developer Network) is much better, and probably easier than the actually W3C spec. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 17, 2013 at 16:18

1 Answer 1

3

Try this:

<script type="text/javascript">
window.onload = function(){
 document.body.innerHTML = 
 document.body.innerHTML.replace(/Attendees/g, "Count");
}
</script>

You need to make sure your script runs after the page has completely loaded: window.onload\

Also check out the replace documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

answered Dec 17, 2013 at 16:12
Sign up to request clarification or add additional context in comments.

6 Comments

If the <script> tag is placed at the end of the <body>, then this won't make a difference.
Thank you!This works on the first page only and will not run through the other layered pages. I need to run it through all layered pages.
Any thoughts on getting it to run through layered pages?
It sounds you are asking a new question. What exactly do you mean by layered pages?
This is on a hard coded registration page so there are layered pages through the registration process. I need to do the same replacement throughout these layers, but each layered page does not its own coding. Sorry if this isn't clear, this old hard coded website is the death of me and I can't wait until we change.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.