SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie:- Search HTML page for a string and replace

    Hi..
    I have some html pages..Page1.html , Page2.html , Page3.html
    Each time these pages loads.. i want a function to be executed taht will search for a particular string and replace it..

    Eg:--Search for "hello" in all pages and replace it with "welcome" in the pages


    var oldGreet="hello"
    var newGreet
    newGreet = oldGreet.replace("hello", "welcome");

    I want this action to take place in all the 3 html pages when they load.
    I dont even know if i am putting my question properly..
    Any help is great help
    Deepa

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you want to do that? Why can't you just edit the pages?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    I have just uploaded a script to http://javascript.about.com/library/blhilite.htm that allows selected words on a page to be highlighted by adding span tags around them. That might give you some ideas on how to do your search and replace.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would have to alunch a function on the onload of these pages ... you therefore could have the function in a separate js file or just add in the pages
    in the body tag:

    Code:
    <script type='text/javascript'>
       function myswap(mytext) {
               var reg
         
             reg = new RegExp("welcome","ig")
             mytext = mytext.replace(reg,"hello")
    
          
          return mytext
       }
    
       
    </script>
    </head>
    
    <body onload="document.body.innerHTML=myswap(document.body.innerHTML)">
    welcome
    </body>

  5. #5
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for all you suggessions.. I will try each one and gety back to you.,,be back in a couple of days..:-D.. i am a slow learner

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you would have to alunch a function on the onload of these pages....or just add in the pages
    in the body tag
    Don't ever put onload events in the <body> tag.

  7. #7
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud View Post
    Don't ever put onload events in the <body> tag.
    I am sorry.. i dont understand.. what am i not supposed to do here?

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,826
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    instead of adding Javascript into the body tag you should put it in a Javascript file linked into the head of the page.

    Instead of <body onload="somefunction(arg)"> in the HTML
    you should use window.onload = function() {somefunction(arg);}; in the Javascript instead - just substitute what you want to run for somefunction(arg)
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for letting me know.. i will try out all the suggesstions and get back..Thknns again..:-)

  10. #10
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Space Forg..I used ur code to replace the :-) wiht the smiley images
    Eg:-
    function myswap(mytext)

    {
    mytext=mytext.replace(":-)","<img src='F:/Smileys/happy.gif'/>");
    mytext=mytext.replace(":-D","<img src='F/Smileys/toothy-grin.gif'/>");
    mytext=mytext.replace(":-(","<img src='F:/Smileys/cry.gif'/>");
    mytext=mytext.replace(":-))","<img src='F/Smileys/lol.gif'/>");
    return mytext
    }
    and the html code
    <html>
    <head>
    <script type='text/javascript' src='F:/Deepa/JScript/Smiley.js'></script>
    </head>

    <body onload="document.body.innerHTML=myswap(document.body.innerHTML)">

    hello there.. .
    ":-)" Angel--":-O" Alien
    <br/>:-( Cry--:-D Toothy grin
    :-) :-) :-)) :-) :-)) :-D</body>
    </html>

    The smiley characters are getting replaced only on the first occurance... why is this so.. Am i supposed to put this in some kind of loop

  11. #11
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's because you're passing a string instead of a RegExp with the case-insensitive and global options set, like SpaceFrog showed in his example.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  12. #12
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was trying to do a generic code for smiley replacement
    But I have here two problems
    the first concerns the onload ...
    it does not work with window.onload ... it works when I put it in the body tag ...

    the second is that it bugs when I try ti put an element of array as a regExp ...
    maybe because of the special characters ?

    Code:
    <script type='text/javascript'>
       function surlign(chaine) {
       
          var smil= new Array()
           smil[0]= new Array()
           smil[1]= new Array()
           smil[2]= new Array()
    	    smil[3]= new Array()
    
          
          smil[0][0]="\:\-\)"
          smil[0][1]="<img src='F:\/Smileys\/happy.gif'\/>"
          smil[1][0]=":-D"
          smil[1][1]="<img src='F/Smileys/toothy-grin.gif'/>"
          smil[2][0]=":-("
          smil[2][1]="<img src='F:/Smileys/cry.gif'/>"
          smil[3][0]=":-))"
          smil[3][1]="<img src='F/Smileys/lol.gif'/>"
    
    alert( smil.length)
    
          var reg
          for(i=0;i<smil.length;i++) {
             reg = new RegExp(smil[i][0],"ig")
             chaine = chaine.replace(reg,smil[i][1])
          }
          
            
     
          return chaine
       }
       
    //-->
    window.onload=document.body.innerHTML=surlign(document.body.innerHTML)
    </script>
    </head>
    
    <body  >
    :-)
    </body>
    
    </html>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •