HTML to Entities script

Discussion in 'JavaScript' started by b1u3eyes, Nov 25, 2011.

  1. #1
    <head>
    <SCRIPT type=text/javascript>
    
    function html2entities(){
    var re=/[(<>"'&]/g
    for (i=0; i<arguments.length; i++)
    [B]arguments[i].value=arguments[i].value.replace(re, function(b){return replacechar(b)})[/B]
    }
    
    function replacechar(match){
    if (match=="<")
    return "&lt;"
    else if (match==">")
    return "&gt;"
    else if (match=="\"")
    return "&quot;"
    else if (match=="'")
    return "'"
    else if (match=="&")
    return "&amp;"
    }
    </SCRIPT>
    </head>
    
    <body>
    <FORM>
    <TEXTAREA style="WIDTH: 400px; HEIGHT: 100px" name=data1></TEXTAREA><BR>
    <INPUT onclick=html2entities(this.form.data1) type=button value="Convert special chars to entities"> 
    </FORM>
    </body>
    </html>
    Code (markup):
    This JavaScript converts special HTML characters to their entities version inside user entered data such as a TEXTAREA before the form is submitted.

    i have some questions. Please help me. I spend over 2 weeks to know it but i can't

    What this code use for???: arguments.value=arguments.value.replace(re, function(b){return replacechar(b)})

    This function function(b){return replacechar(b) without name just include parameter. What they call???

    Parameter b use for what?
     
    b1u3eyes, Nov 25, 2011 IP
  2. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #2
    the function can have multiple parameters "arguments" is a reserved variable in javascript that reads the passed parameters in a function..
    it is an array, so you can loop it for you to be able to access all the parameters passed..

    about function(b){return replacechar(b)..
    it will be the shorthand of..
    
    var temp_func = function(b){
      return replacechar(b)
    };
    arguments[i].value=arguments[i].value.replace(re,temp_func);
    
    Code (markup):

    b is the string found in the arguments.value to be replaced..
     
    JohnnySchultz, Nov 29, 2011 IP
  3. herohana

    herohana Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I want ad rotate script. please help me to get ad banner or ads (script ad) rotating script. :confused:
     
    herohana, Nov 29, 2011 IP
  4. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #4
    you might want to create a separate thread for that..
     
    JohnnySchultz, Dec 15, 2011 IP