Results 1 to 9 of 9

Thread: Javascript Replace()

  1. #1
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript Replace()

    Hello,
    I am have a javascript code I do replace some words of content pages.

    The code is:

    Code:
    function replaceWords()
    {
        var oldWords = new Array("Oradea","Sibiu","Bucuresti");
        var newWords = new Array("<b>Oradea</b>","<b>Sibiu</b>","<b>Bucuresti</b>");
          
        allTableData = document.getElementsByTagName('table');
        allTableHeaders = document.getElementsByTagName('div');
          
        var collections = new Array(allTableData,allTableHeaders);
        for (var k = 0; k < collections.length; ++k )
        {
            for (var i = 0; i < collections[k].length; ++i )
            {  
                if (collections[k][i].innerHTML.indexOf('BODY') == -1)
                {
                    for ( var n = 0; n < oldWords.length; ++n )
                    {
                        var indx = collections[k][i].innerHTML.indexOf(oldWords[n])
                        while (indx != -1)
                        {  
                            var replacement = '';
                            indx = collections[k][i].innerHTML.indexOf(oldWords[n]);
                            replacement = collections[k][i].innerHTML.replace(oldWords[n], newWords[n]);
                            collections[k][i].innerHTML = replacement;
                            break;                  
                        }
                    }
                }
            }
        }
    }
    replaceWords();
    My question is how can I do to not replace my words on the links, h1, h2, h3, images and tag <span>.

    Thanks !

  2. #2
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    2,023
    Thanks
    17
    Thanked 319 Times in 318 Posts
    Blog Entries
    3

    Default

    I don't know js but Brasov is a nice city.

  3. #3
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, Brasov is a very beautiful city.
    Expect continued a solution to my problem if someone knows.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not 100% sure this will work with your markup, but I tested it a number of ways and it seems to fit the bill. The below demo works fine:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <div>Bucuresti, I think Sibiu is a Hot Oradea<br>
    Hello and welcome in Oradea to this great sporting event<br>
    <span>Sibiu</span><br>Bucuresti
    </div>
    <script type="text/javascript">
    
    function replaceWords(){
     var boldWords = new RegExp('(Oradea)|(Sibiu)|(Bucuresti)', 'g'),
     allTableData = document.getElementsByTagName('td'),
     allTableHeaders = document.getElementsByTagName('div'),
     collections = new Array(allTableData,allTableHeaders),
     carve = function(tnm, cn){
      var text = cn.nodeValue.replace(boldWords, '?!$').split('?!$');
      for(var b, n = [], i = 0; i < text.length; ++i){
       n.push(document.createTextNode(text[i]));
       if(tnm[i]){
        b = document.createElement('b');
        b.appendChild(document.createTextNode(tnm[i]));
        n.push(b);
       }
     }
     for (var i = n.length - 1; i > -1; --i)
      i == n.length - 1? cn.parentNode.replaceChild(n[i], cn) : n[i+1].parentNode.insertBefore(n[i], n[i+1]);
     };
     for (var k = collections.length - 1; k > -1; --k)
      for (var cn, tnm, i = collections[k].length - 1; i > -1; --i){
       cn = collections[k][i].childNodes;
       for (var j = cn.length - 1; j > -1; --j)
        if(cn[j].nodeType == 3 && (tnm = cn[j].nodeValue.match(boldWords)))
         carve(tnm, cn[j]);
     }
    };
    replaceWords();
    
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 11-24-2008 at 09:06 PM. Reason: correct code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much for the code works but has some error, for example if we wrote: "Hello and welcome in Oradea to this great sporting event" I make bold text on Oradea but no longer appears that the text is written after that mean "to this great sporting event."

    You can test to see code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <div>Bucuresti, I think Sibiu is a Hot Oradea<br>
    <span>Sibiu</span><br>Bucuresti<br/><br/>
    Hello and welcome in Oradea to this great sporting event.</div>
    <script type="text/javascript">
    
    function replaceWords(){
     var boldWords = new RegExp('(Oradea)|(Sibiu)|(Bucuresti)', 'g'),
     allTableData = document.getElementsByTagName('td'),
     allTableHeaders = document.getElementsByTagName('div'),
     collections = new Array(allTableData,allTableHeaders),
     carve = function(tnm, cn){
      var text = cn.nodeValue.replace(boldWords, '?!$').split('?!$');
      for(var b, n = [], i = 0; i < tnm.length; ++i){
       n.push(document.createTextNode(text[i]));
       b = document.createElement('b');
       b.appendChild(document.createTextNode(tnm[i]));
       n.push(b);
     }
     for (var i = n.length - 1; i > -1; --i)
      i == n.length - 1? cn.parentNode.replaceChild(n[i], cn) : n[i+1].parentNode.insertBefore(n[i], n[i+1]);
     };
     for (var k = collections.length - 1; k > -1; --k)
      for (var cn, tnm, i = collections[k].length - 1; i > -1; --i){
       cn = collections[k][i].childNodes;
       for (var j = cn.length - 1; j > -1; --j)
        if(cn[j].nodeType == 3 && (tnm = cn[j].nodeValue.match(boldWords)))
         carve(tnm, cn[j]);
     }
    };
    replaceWords();
    
    </script>
    </body>
    </html>
    I apologize but I just gave an example with bold words but every word will be a tooltip with a description that will dynamically for each and every word I saw in your code is not possible to do so.

  6. #6
    Join Date
    Nov 2008
    Posts
    40
    Thanks
    2
    Thanked 8 Times in 8 Posts

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by geo100x View Post
    Thank you very much for the code works but has some error, for example if we wrote: "Hello and welcome in Oradea to this great sporting event" I make bold text on Oradea but no longer appears that the text is written after that mean "to this great sporting event."

    . . .


    I apologize but I just gave an example with bold words but every word will be a tooltip with a description that will dynamically for each and every word I saw in your code is not possible to do so.
    OK, well I fixed my code so that it now works (I thought I had already tested a case like that). But I had no idea you wanted a different script than you were asking for. Perhaps MJH's idea will work for you:

    Quote Originally Posted by MJH View Post
    You might like this though:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .toolTip {
    cursor: help;
    font-weight: bold;
    }
    </style>
    </head>
    <body>
    <div>Bucuresti, I think Sibiu is a Hot Oradea.<br>
    Hello and welcome in Oradea to this great sporting event.<br>
    <span>Sibiu</span><br>Bucuresti
    </div>
    <script type="text/javascript">
    
    ;(function(){
     var addTips = {
      Oradea : 'Capital of Bihor County in Crisana, Romania',
      Sibiu : 'Capital of Sibiu County, Located some 282 km NW of Bucharest',
      Bucuresti : 'Capital City, Industrial, and Commercial Center of Romania'
     },
     tipWords = [], allTableData = document.getElementsByTagName('td'),
     allTableHeaders = document.getElementsByTagName('div'),
     collections = [allTableData, allTableHeaders],
     carve = function(tnm, cn){
      var text = cn.nodeValue.replace(tipWords, '?!$').split('?!$');
      for(var tip, n = [], i = 0; i < text.length; ++i){
       n.push(document.createTextNode(text[i]));
       if(tnm[i]){
        tip = document.createElement('span');
        tip.className = 'toolTip';
        tip.title = addTips[tnm[i]];
        tip.appendChild(document.createTextNode(tnm[i]));
        n.push(tip);
       }
     }
     for (var i = n.length - 1; i > -1; --i)
      i == n.length - 1? cn.parentNode.replaceChild(n[i], cn) : n[i+1].parentNode.insertBefore(n[i], n[i+1]);
     };
     for (var p in addTips)
      if (addTips.hasOwnProperty(p))
       tipWords.push(p);
     tipWords = new RegExp('(' + tipWords.join(')|(') + ')', 'g');
     for (var k = collections.length - 1; k > -1; --k)
      for (var cn, tnm, i = collections[k].length - 1; i > -1; --i){
       cn = collections[k][i].childNodes;
       for (var j = cn.length - 1; j > -1; --j)
        if(cn[j].nodeType == 3 && (tnm = cn[j].nodeValue.match(tipWords)))
         carve(tnm, cn[j]);
     }
    })();
    
    </script>
    </body>
    </html>
    Last edited by jscheuer1; 11-24-2008 at 11:19 PM. Reason: add new code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    First, thanks for the help granted by now.

    MJH, you code can not use because I do not have the necessary knowledge to change for my needs.

    I am changed code's the jscheuer1 for what am need but have some error.
    I attached the code because it was too big.

    The archive can be found:
    1) ad.js - Code which replace the words and display tooltips
    2) wz_tooltip.js - Tooltip code
    3) ad.php - This file receives data that are sent by javascript, more specifically data that are sent to the line of code:
    Code:
    myurl var = 'http://localhost/InLineText/ad.php?affID =' + addTips [tnm [s] ];
         tip.setAttribute ( "onmouseover", "return escape (awContent ( '" + myurl +"'));");
    4) index.html - The page is made to replace words.

    The code does not work in IE, not look Tooltips.
    I am like to replace only the exact words, example: If the words is the Bucuresti and Bucurestiul on the word to Bucurestiul do not replace.

    I want to make a kind of ad InLineText.

    How do I code to function even if the files ad.js, ad.php and wz_tooltip.js are on a different server than that index.html is the path to the files ad.js and wz_tooltip.js as in the example of the archive
    For i am want to do so should work because it will be advertising on words.

    If you do not understand something, please tell me.
    Please someone help me with this code if you can, thanks!

  9. #9
    Join Date
    Nov 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    .toolTip {
    cursor: help;
    font-weight: bold;
    }
    </style>
    </head>
    <body>
    <div>Bucuresti, I think Sibiu is a Hot Oradea.<br>
    Hello and welcome in Oradea to this great sporting event.<br>
    <span>Sibiu</span><br>Bucuresti
    </div>
    <script type="text/javascript">
    
    ;(function(){
     var addTips = {
      Oradea : 'Capital of Bihor County in Crisana, Romania',
      Sibiu : 'Capital of Sibiu County, Located some 282 km NW of Bucharest',
      Bucuresti : 'Capital City, Industrial, and Commercial Center of Romania'
     },
     tipWords = [], allTableData = document.getElementsByTagName('td'),
     allTableHeaders = document.getElementsByTagName('div'),
     collections = [allTableData, allTableHeaders],
     carve = function(tnm, cn){
      var text = cn.nodeValue.replace(tipWords, '?!$').split('?!$');
      for(var tip, n = [], i = 0; i < text.length; ++i){
       n.push(document.createTextNode(text[i]));
       if(tnm[i]){
        tip = document.createElement('span');
        tip.className = 'toolTip';
        tip.title = addTips[tnm[i]];
        tip.appendChild(document.createTextNode(tnm[i]));
        n.push(tip);
       }
     }
     for (var i = n.length - 1; i > -1; --i)
      i == n.length - 1? cn.parentNode.replaceChild(n[i], cn) : n[i+1].parentNode.insertBefore(n[i], n[i+1]);
     };
     for (var p in addTips)
      if (addTips.hasOwnProperty(p))
       tipWords.push(p);
     tipWords = new RegExp('(' + tipWords.join(')|(') + ')', 'g');
     for (var k = collections.length - 1; k > -1; --k)
      for (var cn, tnm, i = collections[k].length - 1; i > -1; --i){
       cn = collections[k][i].childNodes;
       for (var j = cn.length - 1; j > -1; --j)
        if(cn[j].nodeType == 3 && (tnm = cn[j].nodeValue.match(tipWords)))
         carve(tnm, cn[j]);
     }
    })();
    
    </script>
    </body>
    </html>
    I can help someone to change the script so that you do the following:

    1) Words in text Bucuresti and Oradea repeated the script I do replace all the words, I would like to replace a word only once.

    2)I'd like to be replace only if the word is exactly and not replace the word if for example found Bucurestilor i like to not replace with Bucuresti.

    I would be deeply grateful if you can help me, thank you

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
  •