change text: foo -> bar

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan Diebolt

    change text: foo -> bar

    I need some script that change "foo" to "bar" anywhere "foo" occurs on
    the page once the page is loaded. Any idea how?
  • Evertjan.

    #2
    Re: change text: foo -> bar

    Dan Diebolt wrote on 04 sep 2003 in comp.lang.javas cript:
    [color=blue]
    > I need some script that change "foo" to "bar" anywhere "foo" occurs on
    > the page once the page is loaded. Any idea how?
    >[/color]

    IE:

    <body onload='mybody. innerText=mybod y.innerText.rep lace(/foo/g,"bar")'>
    <div id="mybody">
    blah foo<br>
    blah foo
    </div>
    </body>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Dan Diebolt

      #3
      Re: change text: foo -&gt; bar


      <body onload='mybody. innertext=mybod y.innertext.rep lace(/foo/g,"bar")'>
      <div id="mybody">
      blah foo<br>
      blah foo
      </div>
      </body>

      I don't have a <div> to grab "mybody" as a handle. I have to simply make
      the change to whatever text is within <body>.



      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Evertjan.

        #4
        Re: change text: foo -&gt; bar

        Dan Diebolt wrote on 04 sep 2003 in comp.lang.javas cript:
        [color=blue]
        > <body onload='mybody. innertext=mybod y.innertext.rep lace(/foo/g,"bar")'>
        > <div id="mybody">
        > blah foo<br>
        > blah foo
        > </div>
        > </body>
        >
        > I don't have a <div> to grab "mybody" as a handle. I have to simply make
        > the change to whatever text is within <body>.
        >
        >[/color]

        1/ It is not innertext, but innerText

        2/ Why do you let yourself be constricted that way ? It seems you want to
        change somone elses work. This could be unetical. The final p[ossibilitiy
        is called "html-mining".

        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: change text: foo -&gt; bar

          dandiebolt@yaho o.com (Dan Diebolt) writes:
          [color=blue]
          > I need some script that change "foo" to "bar" anywhere "foo" occurs on
          > the page once the page is loaded. Any idea how?[/color]

          Best guess is to run through the DOM tree:
          ---
          function crawlDOM(node,f node,ftext) {
          switch (node.nodeType) {
          case 1: // normal node
          for(var i = 0;i<node.childN odes.length; i++) {
          crawlDOM(node.c hildNodes[i],fnode,ftext);
          }
          fnode(node);
          break;
          case 3: // text node
          ftext(node);
          break;
          }
          }
          ---

          Then you can do:
          ---
          crawlDOM(docume nt.body,
          function (){},
          function (tnode) {
          tnode.nodeValue = tnode.nodeValue .replace(/foo/g,"bar");
          });
          ---

          You just have to pray that the document tree really is a tree :)

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...