Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Assign entity to form text trouble

Reply
Thread Tools

Assign entity to form text trouble

 
 
google@quietbay.net
Guest
Posts: n/a
 
      11-02-2005
It would seem that when I assign an HTML entity to a form text input
using "inline" javascript that it will display properly. But when
trying to set it via a function call, the entity text shows in the
field instead of the symbol it represents. The following example
produces these results on Opera 8.5 and IE 6.0

I am expecting the same results whichever way I set the contents of
"myText". Why, when choosing "function" does "&copy" show in the field
instead of the copy symbol? If there is something obviously wrong... I
cannot see it. Any comments are appreciated.

<head>
<title>My Test</title>
</head>
<body>
<script language="JavaScript" type="text/javascript">
function set_it(obj)
{
obj.form.myText.value='&copy;';
}
</script>
<form action="#" method="get">
<input name="myText" type="text" value="&copy;" />
<input name="btn01" type="button" value="inline"
onClick="this.form.myText.value='&copy;';"/>
<input name="btn02" type="button" value="function"
onClick="set_it(this);"/>
</form>
</body>
</html>

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      11-02-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> It would seem that when I assign an HTML entity to a form text
> input using "inline" javascript that it will display properly.
> But when trying to set it via a function call, the entity text
> shows in the field instead of the symbol it represents. The
> following example produces these results on Opera 8.5 and
> IE 6.0
>
> I am expecting the same results whichever way I set the
> contents of "myText". Why, when choosing "function" does
> "&copy" show in the field instead of the copy symbol? If
> there is something obviously wrong... I cannot see it.
> Any comments are appreciated.
>
> <head>
> <title>My Test</title>
> </head>
> <body>
> <script language="JavaScript" type="text/javascript">
> function set_it(obj)
> {
> obj.form.myText.value='&copy;';
> }
> </script>
> <form action="#" method="get">
> <input name="myText" type="text" value="&copy;" />
> <input name="btn01" type="button" value="inline"
> onClick="this.form.myText.value='&copy;';"/>
> <input name="btn02" type="button" value="function"
> onClick="set_it(this);"/>
> </form>
> </body>
> </html>


You are experiencing the difference between PCDATA and CDATA. The
contents of a SCRIPT element are specified in HTML (but not XHTML) as
being CDATA; literal character data where the only significant character
sequence is ' </', which may be taken as terminating the CDATA (this is
superficial description, refer to the section of the HTML specification
on 'types' for more detail).

On the other hand the contents of the value assigned to an event
handling attribute is PCDATA and is parsed by the HTML parser. The
parser will observe entities that it recognises, such as &copy;, and
substitute them with a known character. This will happen before any code
in the even handling attribute is shown to the javascript interpreter.

As you are using the copyright symbol it may be worth mentioning that
there was a release of Netscape 6.2 (hopefully now long dead) that would
refuse to execute javascript source that contained a literal copyright
symbol).

In any event, you should be able to use a literal copyright symbol in a
string literal in CDATA, but that might not be a good idea. So instead
you should probably use a javascript escape sequence in that context:
Hex escape = \xA9 Unicode escape = \u00A9

You can also use the javascript escape sequence(s) in the event handling
attribute value, as it would mean nothing to the HTML parser.

Richard.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Entity, problem with entity key ThatsIT.net.au ASP .Net 1 09-07-2009 02:20 AM
Entity Framework - Reassigning child entity's parent Norm ASP .Net 3 07-06-2009 07:28 PM
OpenJPA persist(...) doesn't assign the Id to the Entity instance Giovanni Azua Java 1 02-19-2009 07:23 PM
How to relate a SQL based entity with an Object based entity in Entity Framework markla ASP .Net 1 10-06-2008 09:42 AM
Entity Name or Entity Number? Samuel van Laere HTML 4 02-24-2007 10:11 PM



Advertisments