1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Working on HTML Enities in Javascript

Discussion in 'JavaScript and AJAX' started by pradeep, Feb 24, 2009.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:

    Introduction


    Some characters are reserved in HTML. For example, you cannot use the greater than or less than signs within your text because the browser could mistake them for markup.

    If we want the browser to actually display these characters we must insert character entities in the HTML source.

    A character entity looks like this: &entity_name; OR &#entity_number;

    To display a less than sign we must write: &lt; or <

    The advantage of using an entity name instead of a number is that the name often is easier to remember. However, the disadvantage is that browsers may not support all entity names (while the support for entity numbers is very good).

    The Code



    I have made to subroutines to enityfy and unentityfy strings. You'll need jQuery to use this code.

    Code:
    var str = '<div>s& dj ? ssss</div>'; 
     
    alert(jsHTML_encode(str)); 
    var str1 = jsHTML_encode(str); 
    alert(jsHTML_decode(str1)); 
     
     
     
    function jsHTML_encode(str){ 
        return($('<div/>').text(str).html());     
    } 
     
    function jsHTML_decode(str){ 
        return($('<div/>').html(str).text());     
    } 
    

    References


    http://www.w3schools.com/HTML/html_entities.asp
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
  3. Mandy

    Mandy New Member

    Joined:
    Apr 22, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    How can we create design in html with the help of java script ?
     
  4. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    In this coding Working on HTML Enities in Javascript

    Code:
    <input type="text" id="x" value="">
    <script type="text/javascript">
    window.onload=function()
    {
        document.getElementById('x').value='&amp;';
    }
    </script>
    
     
  5. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    @gkumar: for this the html element with id 'x' has to be present, and moreover it just sets the html entity.
     

Share This Page