473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

Jul 20 '05 #1
3 38745
> 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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: francescomoi | last post by:
Hi. I'm trying to store a text within a MySQL field (v 3.23.58) by using MySQLdb (v 1.2.1c3). The text is: "telephone..." (note the last character) And I get this error message:...
3
by: Ricky | last post by:
Is there any way to detect if a field has been encoded before I decode a field.
5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
3
by: Peter | last post by:
Hi, I try to make up a javascript string which contains numeric numbers in any positions. For example, I want to make a string: secretcode, where secretcode.charAt(0)==(-21),...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
7
by: erikcw | last post by:
Hi, I'm trying to build a SQL string sql = """INSERT INTO ag ('cid', 'ag', 'test') VALUES(%i, %s, %d)""", (cid, ag, self.data) It raises this error: AttributeError: 'tuple' object has no...
5
by: Timothy Madden | last post by:
Hello Is there a function that will allow me to output text written in utf-8 (from db for example) if my document has Content-Type: text/html; charset=ISO-8859-1 I mean htmlspecialchars()...
6
by: 7stud | last post by:
s1 = "hello" s2 = s1.encode("utf-8") s1 = "an accented 'e': \xc3\xa9" s2 = s1.encode("utf-8") The last line produces the error: --- Traceback (most recent call last):
4
sanjib65
by: sanjib65 | last post by:
Whenever I take user's input through TextBox or anything else, it's good practice to use Html.Encode(TextBox1.Text) for the security purpose. But is it neccessary now as ASP.NET 2.0 has strengthened...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.