Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Character Replacing in HTML (Cross-Browser Code) How?

    Hi ..

    I have an adea to replace some charactor by another one in web pages by JavaScript.

    Example(1) - One Charactor Replacing :
    ----------------------------------------------------------------
    I need to replace:
    any "#" charactor to "@"
    any "a" to "A",
    any "\n" to " ",
    and any "~" to "&".

    Example(2) - Word or Phrase Replacing :
    ----------------------------------------------------------------
    Replacing a list or array of bad words to other words.


    I don't know how I can do that for all document items like: <body>, <td>, <th>, <div>, <span>, <p> and so on.
    but I need to check a full HTML page not only those tags.
    Also I need a cross-browser code using DOM.

    I think we can use something like this:
    Code:
      
    item.innerHTML
    
    or:
    
    document.layers.innerHTML // for Geko engine browsers.
    document.all.innerHTML // for IE browser.
    I can do this simply in PHP language functions like str_replace() but it's a server-side language, I want a cleint-side method by JavaScript.
    Can any JavaScript professional help me ?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Example 1.

    The general form of the regex is
    word = word.replace (/\a/g, "A"); // replace all instances of a by A
    word = word.replace (/\~/g, "@");
    // replace all instances of ~ by @


    Example 2.

    See:
    http://www.js-examples.com/page/java...le.html?id=964

    http://www.irt.org/script/229.htm

  • #3
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks I checked links, but nothing useful for me yet.
    Can you explain more about your code and tel me how to use it for Example 1 ?
    Last edited by alMubarmij; 12-31-2006 at 03:54 PM.

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    loop through all elements by calling a function recursively like

    Code:
    function scanNodes(node) {
      var i;
      for(i = 0; i < node.childNodes.length; i++)
        scanNodes(node.childNodes[i]);
    }
    Then while looking through all those nodes, check if theyre text nodes by checking their nodeName attribute, and if it is remove this text node and re-add with the replaced characters.

    Code:
    function scanNodes(node) {
     
      var i;
    
      if(node.nodeName == '#text') {
        
        /* get the old text held by the text node */
    
        var oldText = node.nodeValue;
        
        /* create a new text node with the characters replaced, in the case below  
         * the character a is replaced by A, look up regex with google if you need 
         * know more
         */
       
        var newNode = document.createTextNode(oldText.replace(/a/g, 'A'));
        
        /* insert the new node before the old node, to assure it will be in the correct spot */    
    
        node.parentNode.insertBefore(newNode, node);
        
        /* remove the old node */
         
        node.parentNode.removeChild(node);
        
        /* bail since text nodes dont have children */
    
        return;
      }
    
      /* recursively loop through all child nodes */  
    
      for(i = 0; i < node.childNodes.length; i++)
        scanNodes(node.childNodes[i]);
    }
    In order to make my example work youd call scanNodes on the body element

    Code:
    <body onload="scanNodes(document.body)">
    http://demo.vegui.org - vegUI AJAX framework
    http://www.landsofkazram.com - browser based graphical MMORPG

  • #5
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry I am not advanced in JavaScript.

    Can you put a full page form my example1:
    Example(1) - One Charactor Replacing :
    --------------------------------------
    I need to replace:
    any "#" charactor to "@"
    any "a" to "A",
    any "\n" to "&nbsp;",
    and any "~" to "&".

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    What is the point of this? What are you trying to achieve? You cannot make changes to other peoples' web pages.

  • #7
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know that, I want the code for my own web-site !

  • #8
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all for your support.

    but I have another problem now, and I hope to continue your help.

    I want now to replace the each digite number (1, 2, 3 ...) by Unicode codes like:
    Code:
    &#1500 ;
    &#1220 ;
    &#1200 ;
    &#1202 ;
    &#1203 ;
    ..etc.
    if I used the previous code it will replace the Unicode code numbers.

    do you have any solution for that ?
    Last edited by alMubarmij; 01-10-2007 at 06:21 PM.

  • #9
    New Coder
    Join Date
    Apr 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    More Explaination:

    I want something doing like this:
    Code:
    0 = &#1200 ;
    1 = &#1500 ;
    2 = &#1220 ;
    3 = &#1201 ;
    4 = &#1202 ;
    5 = &#1203 ;
    6 = &#1204 ;
    7 = &#1205 ;
    8 = &#1206 ;
    9 = &#1207 ;
    Note:
    I put a space before ";" because the forum treat with the code as a HTML, and it will replace it.


  •  

    Posting Permissions

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