Re: Coding and decoding HTML entities
- From: Dennis Marks <denmarks@xxxxxxxxx>
- Date: Fri, 22 Aug 2008 17:38:09 -0700
Anthony Jones wrote:
"Dennis Marks" <denmarks@xxxxxxxxx> wrote in message
news:%23A1GecuAJHA.1396@xxxxxxxxxxxxxxxxxxxxxxx
Dennis Marks wrote:Martin Honnen wrote:The following works. Is there a better way.Dennis Marks wrote:I have tried various versions of your code and can't get it to work. CanIs there a way in Javascript to convert from a character to its entityWell if you are talking about JavaScript used in the browser in a HTML
and from its entity to a character? I wish to do it in a form. For
example someone enters ♥ into the form and a heart is shown in
the output.
document then innerHTML helps e.g.
var span = document.createElement('span');
span.innerHTML = '♥';
someElement.appendChild(span);
> Also the reverse where a heart in entered into the form and♥ is displayed.For that you would need to set up some data structure that maps
characters to entity names.
you give the exact code assuming a form named "Form3", input field
"Field6", and output field "Field7"?
var str1 = document.theForm3.field6.value;
var str2 = document.createElement("str2");
Its not a good idea to use "str2" as an element name, use an appropriate
HTML element such as "span" or "div".
str2.innerHTML = str1;
document.theForm3.field7.value = str2.innerHTML;
Basically you are allowing the user to enter HTML code into a field that you
then render on your page. The danger is that this could allow someone to do
something malicious. A user could enter something like this:-
<img src="reference to image" onload="malicious jscript here" />
or some other similar thing.
You should therefore reject the < character in any input the user will need
to use the < entity for a visible < in the rendering.
Isn't anything malicious only going to cause problems on the person's own view of the page? I don't see how it could affect the source or anyone else's view of the page. The form goes nowhere. It just displays.
Here is my page.
http://www.geocities.com/denmarks/SpecialChars.html
.
- Follow-Ups:
- Re: Coding and decoding HTML entities
- From: Joe Fawcett
- Re: Coding and decoding HTML entities
- References:
- Re: Coding and decoding HTML entities
- From: Martin Honnen
- Re: Coding and decoding HTML entities
- From: Anthony Jones
- Re: Coding and decoding HTML entities
- Prev by Date: Re: XHR problem in IE6 (long)
- Next by Date: Re: Coding and decoding HTML entities
- Previous by thread: Re: Coding and decoding HTML entities
- Next by thread: Re: Coding and decoding HTML entities
- Index(es):
Relevant Pages
|