423,095 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,095 IT Pros & Developers. It's quick & easy.

encode html with js

P: n/a
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

Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
Jul 20 '05 #2

P: n/a
> 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
Jul 20 '05 #3

P: n/a
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;','','&N tilde;','','&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;','','&e th;','','&euml;','','&frac12;','','&frac14;',' ','&frac34;','','&gt;','>','&gt','>','&iacute;',' ','&icirc;','','&iexcl;','','&igrave;','','&iq uest;','','&iuml;','','&laquo;','','&lt;','<',' &lt','<','&mdash;','','&micro;','','&middot;',' ','&ndash;','','&not;','','&ntilde;','','&oacut e;','','&ocirc;','','&ograve;','','&
oslash;','','&otilde;','','&ouml;','','&para;', '','&plusmn;','','&pound;','','&quot;','\"','&r aquo;','','&reg;','','&sect;','','­','*','&sup1 ;','','&sup2;','','&sup3;','','&szlig;','','&t horn;','','&tilde;','','&trade;','','&uacute;', '','&ucirc;','','&ugrave;','','&uuml;','','&ya cute;','','&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[i])
return (text);
}

cheers ralphie

us**@domain.invalid wrote:
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


Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.