encode html with js

U

user

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
 
M

Markus Ernst

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.

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
 
M

Markus Ernst

basicly i know what you mean.. though the point i want to have this is
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)

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>Unbenanntes Dokument</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" 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="document.myform.myoutput.value=(makehtml(document.myform.myinput.va
lue))">
</p>
</form>
</body>
</html>


hth
Markus
 
U

user

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(eval('/&/g'), '&amp;');
fromTo = new
Array('&AElig;','Æ','&Aacute;','Á','&Acirc;','Â','&Agrave;','À','&Aring;','Å','&Atilde;','Ã','&Auml;','Ä','&Ccedil;','Ç','&ETH;','Ð','&Eacute;','É','&Ecirc;','Ê','&Egrave;','È','&Euml;','Ë','&Iacute;','Í','&Icirc;','Î','&Igrave;','Ì','&Iuml;','Ï','&Ntilde;','Ñ','&Oacute;','Ó','&Ocirc;','Ô','&Ograve;','Ò','&Oslash;','Ø','&Otilde;','Õ','&Ouml;','Ö','&THORN;','Þ','&Uacute;','Ú','&Ucirc;','Û','&Ugrave;','Ù','&Uuml;','Ü','&Yacute;','Ý','&aacute;','á','&acirc;','â','&aelig;','æ','&agrave;','à','&aring;','å','&atilde;','ã','&auml;','ä','&brvbar;','¦','&ccedil;','ç','&cent;','¢','&copy;','©','&deg;','°','&eacute;','é','&ecirc;','ê','&egrave;','è','&eth;','ð','&euml;','ë','&frac12;','½','&frac14;','¼','&frac34;','¾','&gt;','>','&gt','>','&iacute;','í','&icirc;','î','&iexcl;','¡','&igrave;','ì','&iquest;','¿','&iuml;','ï','&laquo;','«','&lt;','<','&lt','<','&mdash;','—','&micro;','µ','&middot;','·','&ndash;','–','&not;','¬','&ntilde;','ñ','&oacute;','ó','&ocirc;','ô','&ograve;','ò','&
oslash;','ø','&otilde;','õ','&ouml;','ö','&para;','¶','&plusmn;','±','&pound;','£','&quot;','\"','&raquo;','»','&reg;','®','&sect;','§','&shy;','­','&sup1;','¹','&sup2;','²','&sup3;','³','&szlig;','ß','&thorn;','þ','&tilde;','˜','&trade;','™','&uacute;','ú','&ucirc;','û','&ugrave;','ù','&uuml;','ü','&yacute;','ý','&yen;','¥','&yuml;','ÿ');

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

cheers ralphie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top