encode html with js

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • user@domain.invalid

    encode html with js

    hi there

    has anyone of you writte a function to encode html from like '&' ->
    '&' and likes to share it with me.. or can anybody give me a hint
    how to set up something like that.

    cheers me.

    ralphie

  • Markus Ernst

    #2
    Re: encode html with js

    > has anyone of you writte a function to encode html from like '&' ->[color=blue]
    > '&' and likes to share it with me.. or can anybody give me a hint
    > how to set up something like that.[/color]

    I am not sure if this is a good idea at all - the point of using the html
    entities (like &) is to make sure that every browser recognizes the
    correct characters regardless where in the world it is used and with what
    text encodings it is configured.

    If the browser recognizes the special character there is no use to convert
    it into an entity anymore, if not the conversion will not work anyway.

    If you write the html code of your pages yourself you can just type the
    correct entities; which will be done automatically if you use a tool like
    Dreamweaver. If your content comes out of a database it will be a good idea
    to convert it on the server side. Some scripting languages provide functions
    for that. In PHP for example you have the function htmlentities() that
    converts your text.

    hth
    Markus


    Comment

    • Markus Ernst

      #3
      Re: encode html with js

      > basicly i know what you mean.. though the point i want to have this is[color=blue]
      > that i made a little dhtml-admin screen wich produces "copy and paste"
      > source code.. and i'd like to provide the correct source.. becasue i
      > don't know in what kind of editor it will be pasted later on (so i also
      > cannot be sure that the editor would replace the chars)[/color]

      Ok I see what you mean. You don't have to replace all special characters in
      this case; the most important ones are < > &

      You can do this with the replace function in Javascript; I wrote a function
      "makehtml" for you. To show you how you can apply it I copy the code of an
      entire html document below. You can add lines to the function, as for ä
      and so on if you like; to show you I added 3 lines that replace the new line
      by a <br> tag.


      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>Unbenann tes Dokument</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <script language="JavaS cript" type="text/JavaScript">
      <!--
      function makehtml(text) {
      var textneu = text.replace(/&/,"&amp;");
      textneu = textneu.replace (/</,"&lt;");
      textneu = textneu.replace (/>/,"&gt;");
      textneu = textneu.replace (/\r\n/,"<br>");
      textneu = textneu.replace (/\n/,"<br>");
      textneu = textneu.replace (/\r/,"<br>");
      return(textneu) ;
      }
      //-->
      </script>
      </head>

      <body>
      <form name="myform" method="get" action="">
      <p>
      <textarea name="myinput" cols="40" rows="5" id="myinput"></textarea>
      </p>
      <p>
      <textarea name="myoutput" cols="40" rows="5" id="myoutput"> </textarea>
      </p>
      <p>
      <input name="show" type="button" id="show" value="show"
      onClick="docume nt.myform.myout put.value=(make html(document.m yform.myinput.v a
      lue))">
      </p>
      </form>
      </body>
      </html>


      hth
      Markus


      Comment

      • user@domain.invalid

        #4
        Re: encode html with js

        hy there

        i finally did it like that.. maybe it is useful for somebody.

        /*
        function replaces extended characters of 'text' with its character
        entities.
        call the function by default with output = false. if you switch it
        to true
        it will format the source code for output in a textarea.
        */
        function replaceExtChars (text,output) {
        text = text.replace(ev al('/&/g'), '&amp;');
        fromTo = new
        Array('&AElig;' ,'Æ','&Aacute;' ,'Á','&Acirc;', 'Â','&Agrave;', 'À','&Aring;',' Å','&Atilde;',' Ã','&Auml;','Ä' ,'&Ccedil;','Ç' ,'&ETH;','Ð','& Eacute;','É','& Ecirc;','Ê','&E grave;','È','&E uml;','Ë','&Iac ute;','Í','&Ici rc;','Î','&Igra ve;','Ì','&Iuml ;','Ï','&Ntilde ;','Ñ','&Oacute ;','Ó','&Ocirc; ','Ô','&Ograve; ','Ò','&Oslash; ','Ø','&Otilde; ','Õ','&Ouml;', 'Ö','&THORN;',' Þ','&Uacute;',' Ú','&Ucirc;','Û ','&Ugrave;','Ù ','&Uuml;','Ü', '&Yacute;','Ý', '&aacute;','á', '&acirc;','â',' &aelig;','æ','& agrave;','à','& aring;','å','&a tilde;','ã','&a uml;','ä','&brv bar;','¦','&cce dil;','ç','&cen t;','¢','&copy; ','©','&deg;',' °','&eacute;',' é','&ecirc;','ê ','&egrave;','è ','&eth;','ð',' &euml;','ë','&f rac12;','½','&f rac14;','¼','&f rac34;','¾','&g t;','>','&gt',' >','&iacute;',' í','&icirc;','î ','&iexcl;','¡' ,'&igrave;','ì' ,'&iquest;','¿' ,'&iuml;','ï',' &laquo;','«','& lt;','<','&lt', '<','&mdash;',' —','&micro;','µ ','&middot;','· ','&ndash;','–' ,'&not;','¬','& ntilde;','ñ','& oacute;','ó','& ocirc;','ô','&o grave;','ò','&
        oslash;','ø','& otilde;','õ','& ouml;','ö','&pa ra;','¶','&plus mn;','±','&poun d;','£','&quot; ','\"','&raquo; ','»','&reg;',' ®','&sect;','§' ,'­','­','&sup1 ;','¹','&sup2;' ,'²','&sup3;',' ³','&szlig;','ß ','&thorn;','þ' ,'&tilde;','˜', '&trade;','™',' &uacute;','ú',' &ucirc;','û','& ugrave;','ù','& uuml;','ü','&ya cute;','ý','&ye n;','¥','&yuml; ','ÿ');

        if (output) {
        fromTo[fromTo.length] = '&amp;';
        fromTo[fromTo.length] = '&';
        }
        for (i=0; i < fromTo.length; i=i+2)
        text = text.replace(ev al('/'+fromTo[i+1]+'/g'), fromTo[i])
        return (text);
        }

        cheers ralphie

        user@domain.inv alid wrote:
        [color=blue]
        > hi there
        >
        > has anyone of you writte a function to encode html from like '&' ->
        > '&amp;' and likes to share it with me.. or can anybody give me a hint
        > how to set up something like that.
        >
        > cheers me.
        >
        > ralphie
        >[/color]

        Comment

        Working...