473,386 Members | 1,867 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,386 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 38748
> 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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.