Assign entity to form text trouble

Discussion in 'Javascript' started by google@quietbay.net, Nov 2, 2005.

  1. Guest

    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>
    , Nov 2, 2005
    #1
    1. Advertising

  2. 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.
    Richard Cornford, Nov 2, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuel van Laere

    Entity Name or Entity Number?

    Samuel van Laere, Feb 24, 2007, in forum: HTML
    Replies:
    4
    Views:
    1,617
    Jukka K. Korpela
    Feb 24, 2007
  2. markla
    Replies:
    1
    Views:
    540
    Steven Cheng
    Oct 6, 2008
  3. Giovanni Azua
    Replies:
    1
    Views:
    1,147
    Giovanni Azua
    Feb 19, 2009
  4. Norm
    Replies:
    3
    Views:
    2,698
  5. ThatsIT.net.au

    Entity, problem with entity key

    ThatsIT.net.au, Sep 6, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    1,190
    ThatsIT.net.au
    Sep 7, 2009
Loading...

Share This Page