473,383 Members | 1,798 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,383 software developers and data experts.

createTextNode/innerHTML and special character handling

Hello,

I was wondering what is the correct way to handle special characters
via javascript and the DOM. I would like to avoid document.write and
innerHTML. What I am doing is dynamically creating options for a
select. The innerHTML example below works for firefox and internet
explorer, but is this the accepted way of dynamically adding special
characters.

option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));
option.innerHTML = "é - example";

Thanks,
Dan Andrews

Jun 4 '07 #1
4 19497
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));
JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 4 '07 #2
On Jun 4, 7:18 pm, Martin Honnen <mahotr...@yahoo.dewrote:
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Yes, and createTextNode( "&eacute; - ..." ) will create text
"&eacute;" visible in the option (which you didn't want, for granted).
What you don't see difference between are the regular text fragments
and xml entities - entities are elements in xml which are recognized
by the famous "&" and ";" delimiters. What you should do is find the
javascript function that can make entities e.g.
document.createEntityReference( "eacute" ), but this I had problems
with - got exceptions etc. in some previous work, so I stuck with the
innerHTML property. I think you have to declare the entity first, then
get this reference etc. Anyway, if you get any idea how this works -
share it with us ;-)

Take a look at this, it probably contains the needed information
*somewhere* inside, but I don't have much time at the moment to read
all of it. Good luck:
http://www.w3.org/TR/2004/REC-DOM-Le...0407/core.html

Jun 4 '07 #3
On Jun 4, 11:18 am, Martin Honnen <mahotr...@yahoo.dewrote:
Dan Andrews wrote:
option = document.createElement("OPTION");
// option.appendChild(document.createTextNode("&eacut e; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendChild(document.createTextNode('é - example'));
Then there is String.fromCharCode e.g.
document.createTextNode(String.fromCharCode(233) + ' - example')
and there are Unicode escape sequences
document.createTextNode('\u00E9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Thanks this is very good to know, however, I don't quite have
literals. I have both entity references and some unicode ("Anhui
\u5b89\u5fbd", "&Ntilde;uble",...) in my real situation. The text is
actually coming from a database via an XMLHttpRequest object and the
responseText. I believe these literals are interpreted a bit
differently when they are hard coded as in your '\u00E9 - example'
versus when they are sent across the wire in an XMLHttpRequest, or is
there a way to specify the characterset in an XMLHttpRequest. The
innerHTML is working well if I convert \u5b89 to &#xu5b89;, but I
still would like to avoid innerHTML.

[snip]
request = new XMLHttpRequest();
request.open("GET", "countries?fn=regions&id=" +
countryTypeIdSelected, true);
request.onreadystatechange = function() {
var i;
var array;
var option;
var select = document.getElementById("region");
var cdata;
if (request.readyState == 4) {
array = request.responseText.split(',');
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option =
document.createElement("OPTION");
option.setAttribute("value", "" +
array[i]);
if(countryTypeIdSelected && countryTypeIdSelected
=== array[i]){
option.setAttribute("selected", "selected");
}
//option.appendChild(document.createTextNode("" +
array[i+1]));
option.innerHTML = "" + array[i+1];
select.appendChild(option);
select.disabled = false;
}
}
[snip]

Jun 4 '07 #4
ASM
Dan Andrews a écrit :
>
still would like to avoid innerHTML.

[snip]
request = new XMLHttpRequest();
request.open("GET", "countries?fn=regions&id=" +
countryTypeIdSelected, true);
request.onreadystatechange = function() {
var i;
var array;
var option;
var select = document.getElementById("region");
var cdata;
if (request.readyState == 4) {
array = request.responseText.split(',');
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option = new Option(array[i+1],array[i]);
if(countryTypeIdSelected &&
countryTypeIdSelected === array[i])
option.selected = true;
select.options[select.length] = option;
}
select.disabled = false;
}
}
[snip]
It is not because you use XMLHttpRequest that old and eficient JS is dead.

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Jun 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
2
by: Avi U | last post by:
I'm creating a DOM document and I'm looking for a way to get an exception when I use ileagal characters. The method DOMElement->createElement() throw exception when I have an ileagal unicode...
11
by: DKM | last post by:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a script as foolows: str = "<b>Hello World!!!</b>"; ele = documeny.getElementById("pid");...
9
by: Hallvard B Furuseth | last post by:
Why does the FAQ (Q 4.15) recommend innerHTML when so many here say one should use createElement(), replaceChild() etc? Also, why does the "Alternative DynWrite function" at...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
50
by: Randy Webb | last post by:
When running this code in IE7: function insertScript() { var newScript = document.createElement('script'); newScript.type = "text/javascript"; var s = document.createTextNode("alert('hi');");...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
1
kaleeswaran
by: kaleeswaran | last post by:
Hi! i am handling password generation module..... i need to force the user enter the special character in their password thing.so i need to handle the special character.so...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.