...

View Full Version : Peculiarity with entities



beetle
02-13-2003, 04:37 PM
For the sake of this post, I'll just show the basics of this "problem". Here's a simple function...
function makeButton( val )
{
var b = document.createElement( "input" );
b.type = "button";
b.value = val;
document.body.appendChild( b );
}Just makes a button with the specified value, right? Well, if I want the value to contain an entity, such as » i just pass it that when I call the function
makeButton( "»" );I know what you're thinking, "What's the problem?", because everyting above works as expected. Here's the weird part: If the entity is hard-coded into the function, it doesn't work. So,
function makeButton()
{
var b = document.createElement( "input" );
b.type = "button";
b.value = "»";
document.body.appendChild( b );
}Dosn't actually insert "»" as the value, but the literal string "»"

Is there some type of translation of the entity that occurs here? Between the function call and execution? I even thought that because the call is in the BODY and the function is in the HEAD that maybe the entity is valuated differently, but after moving the function to the BODY I find this isn't the case.

I'm not really looking for a workaround or anything, just curious as to the nature of this behavior.

:confused:

jkd
02-13-2003, 07:04 PM
You are directly giving button the string, as opposed to the string being passed through the HTML or XML parser (expanding the entity), and then assigned.

In theory, you could use document.createEntityReference('#187'), but not even Mozilla implements that method.

Since you know it is #187 however, you can just parse out the &# and the ; from it, and escape it appropriately with JS:

var num = parseInt(ent.replace(/\D/g, ''), 16);

var esc = '\\x' + ((num < 16) ? '0' : '') + num.toString(16);

var char = eval('"' + esc + '"');

:)

beetle
02-13-2003, 07:13 PM
Interesting, so at what point is the string passed to the function parsed, expanding the entity? Between call and receipt?

jkd
02-13-2003, 07:20 PM
Entities are expanded only once. That is when the XML parser runs through the page. It will not expand or even attempt to parse anything inside <![CDATA ]]> sections, however if your entity is anywhere else, it should be expanded. This includes attribute values, etc.

If you dynamically create a string containing an entity, then assign that string to some visual property, it will not be parsed through the XML parser.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum