473,545 Members | 1,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.create Element("OPTION ");
// option.appendCh ild(document.cr eateTextNode("& eacute; - example"));
option.innerHTM L = "é - example";

Thanks,
Dan Andrews

Jun 4 '07 #1
4 19524
Dan Andrews wrote:
option = document.create Element("OPTION ");
// option.appendCh ild(document.cr eateTextNode("& eacute; - example"));
JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendCh ild(document.cr eateTextNode('é - example'));
Then there is String.fromChar Code e.g.
document.create TextNode(String .fromCharCode(2 33) + ' - example')
and there are Unicode escape sequences
document.create TextNode('\u00E 9 - example')

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jun 4 '07 #2
On Jun 4, 7:18 pm, Martin Honnen <mahotr...@yaho o.dewrote:
Dan Andrews wrote:
option = document.create Element("OPTION ");
// option.appendCh ild(document.cr eateTextNode("& eacute; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendCh ild(document.cr eateTextNode('é - example'));
Then there is String.fromChar Code e.g.
document.create TextNode(String .fromCharCode(2 33) + ' - example')
and there are Unicode escape sequences
document.create TextNode('\u00E 9 - 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.create EntityReference ( "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...@yaho o.dewrote:
Dan Andrews wrote:
option = document.create Element("OPTION ");
// option.appendCh ild(document.cr eateTextNode("& eacute; - example"));

JavaScript supports string literals with Unicode characters so you can
simply use that character in a string literal e.g.
option.appendCh ild(document.cr eateTextNode('é - example'));
Then there is String.fromChar Code e.g.
document.create TextNode(String .fromCharCode(2 33) + ' - example')
and there are Unicode escape sequences
document.create TextNode('\u00E 9 - 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("G ET", "countries?fn=r egions&id=" +
countryTypeIdSe lected, true);
request.onready statechange = function() {
var i;
var array;
var option;
var select = document.getEle mentById("regio n");
var cdata;
if (request.readyS tate == 4) {
array = request.respons eText.split(',' );
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option =
document.create Element("OPTION ");
option.setAttri bute("value", "" +
array[i]);
if(countryTypeI dSelected && countryTypeIdSe lected
=== array[i]){
option.setAttri bute("selected" , "selected") ;
}
//option.appendCh ild(document.cr eateTextNode("" +
array[i+1]));
option.innerHTM L = "" + array[i+1];
select.appendCh ild(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("G ET", "countries?fn=r egions&id=" +
countryTypeIdSe lected, true);
request.onready statechange = function() {
var i;
var array;
var option;
var select = document.getEle mentById("regio n");
var cdata;
if (request.readyS tate == 4) {
array = request.respons eText.split(',' );
if(array.length 1){
for(i=0; i < array.length - 1; i = i + 2){
option = new Option(array[i+1],array[i]);
if(countryTypeI dSelected &&
countryTypeIdSe lected === 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
47796
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
8969
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, mainly because I need the ability to both create and extract zip-files. I've tried a couple of classes found here and there, and they all seem to...
2
2366
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 character: 0x01 (which is good), but when I create a text node by calling: DOMText->createTextNode() I don't get an exception (which is bad). What is...
11
1838
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"); ele.appendChild(document.createTextNode(str)); I expected to see "Hello World!!!" in bold letters, but instead I see "<b>Hello World!!!</b>".
9
8641
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 <http://www.jibbering.com/faq/faq_notes/alt_dynwrite.html> need such a lot of tests to find out if innerHTML assignment actually works, instead of just inserting...
1
4555
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, 2. Text checks for max. length of the field & whether the field is mandatory or optional Example:
50
3988
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');"); newScript.appendChild(s); // problem line document.getElementById("myDiv").appendChild(newScript); }
3
10180
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. This is not a regular expression tutorial. Assumes you are already familiar with basic regular expression concepts and terminology. If not, you may want...
1
2414
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 how do i check if the user enter the special character or not?... waiting for ur's response.... thank...
0
7457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7651
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5962
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.