Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6476293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:48:27+00:00 2026-05-25T06:48:27+00:00

Can the JavaScript command .replace replace text in any webpage? I want to create

  • 0

Can the JavaScript command .replace replace text in any webpage? I want to create a Chrome extension that replaces specific words in any webpage to say something else (example cake instead of pie).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:48:28+00:00Added an answer on May 25, 2026 at 6:48 am

    The .replace method is a string operation, so it’s not immediately simple to run the operation on HTML documents, which are composed of DOM Node objects.

    Use TreeWalker API

    The best way to go through every node in a DOM and replace text in it is to use the document.createTreeWalker method to create a TreeWalker object. This is a practice that is used in a number of Chrome extensions!

    // create a TreeWalker of all text nodes
    var allTextNodes = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT),
        // some temp references for performance
        tmptxt,
        tmpnode,
        // compile the RE and cache the replace string, for performance
        cakeRE = /cake/g,
        replaceValue = "pie";
    
    // iterate through all text nodes
    while (allTextNodes.nextNode()) {
        tmpnode = allTextNodes.currentNode;
        tmptxt = tmpnode.nodeValue;
        tmpnode.nodeValue = tmptxt.replace(cakeRE, replaceValue);
    }
    

    To replace parts of text with another element or to add an element in the middle of text, use DOM splitText, createElement, and insertBefore methods, example.

    See also how to replace multiple strings with multiple other strings.

    Don’t use innerHTML or innerText or jQuery .html()

    // the innerHTML property of any DOM node is a string
    document.body.innerHTML = document.body.innerHTML.replace(/cake/g,'pie')
    
    • It’s generally slower (especially on mobile devices).
    • It effectively removes and replaces the entire DOM, which is not awesome and could have some side effects: it destroys all event listeners attached in JavaScript code (via addEventListener or .onxxxx properties) thus breaking the functionality partially/completely.
    • This is, however, a common, quick, and very dirty way to do it.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an existing PHP, Javascript, or even command line tool that can build
In javascript you can easily create objects and Arrays like so: var aObject =
How can I garble JavaScript code before sending it to client-side? I don't want
I'm transforming a text only menu / command structure into HTML links in JavaScript.
What jQuery/JavaScript command can I use to detect whether the DOM (driven by a
I'm trying to configure a web application that can use client-side JavaScript for localization
So we have a program that the user can use by copying text from
May be duplicate. I would like to write javascript which can execute linux command
can anyone give me a javascript that can do following. I have predefined price
It doesn't look like basic javascript but nor can I use JQuery commands like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.