[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

simple error

Haris Bogdanovi?

12/28/2014 5:12:00 PM

Hi

<td onclick='this.innerHTML = 'X';'>--</td>

Why it won't change to X when I click on it ?

Thanks
24 Answers

Thomas 'PointedEars' Lahn

12/28/2014 5:26:00 PM

0

Haris Bogdanovich wrote:

> <td onclick='this.innerHTML = 'X';'>--</td>
>
> Why it won't change to X when I click on it ?

Possibility: Assuming error correction, the resulting script code is

this.innerHTML = X;

and the identifier â??Xâ? is not defined. (See the error console. See the FAQ
for details on debugging.)

You must not use the same character as markup attribute value delimiter and
script string value delimiter. In this simple case, use either â??'â? and â??"â?,
or â??"â? and â??'â?, respectively. In other cases you can use a character
reference or character entity reference to avoid the offending code, or you
need to move the offending code elsewhere and use that from the attribute
value.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Denis McMahon

12/28/2014 6:46:00 PM

0

On Sun, 28 Dec 2014 18:12:19 +0100, Haris Bogdanovich wrote:

> Hi
>
> <td onclick='this.innerHTML = 'X';'>--</td>
>
> Why it won't change to X when I click on it ?

You're using single quotes (') to delimit a string inside a string
delimited with single quotes. Use double quotes (") for one of the
strings. (`) might also work (it does in my firefox at least).

--
Denis McMahon, denismfmcmahon@gmail.com

Jukka K. Korpela

12/28/2014 7:01:00 PM

0

2014-12-28, 19:25, Lahn wrote:

> Haris Bogdanovich wrote:
>
>> <td onclick='this.innerHTML = 'X';'>--</td>
>>
>> Why it won't change to X when I click on it ?
>
> Possibility: Assuming error correction, the resulting script code is
>
> this.innerHTML = X;
>
> and the identifier â??Xâ? is not defined. (See the error console. See the FAQ
> for details on debugging.)

Apparently Lahn did not bother actually looking at the error console.
Should we bother looking at this â??FAQâ?, which he calls â??the FAQâ??

In reality, as we can see using a browserâ??s Developer Tools, the code is
parsed so that one attribute is

onclick="this.innerHTML = "

and another attribute is

x';'

with no value set. The latter is of course ignored, whereas the first
one sets up an event handler that will terminate in an error (an
assignment without right-hand-side).

An HTML validator issues several error messages about the construct. The
error is really at the HTML markup level.

The problem is caused by an attempt to use the Ascii apostrophe (')
inside an attribute value delimited by Ascii apostrophes. That just
canâ??t work. There are various alternative ways to deal with this, such as

1) Use Ascii quotation marks (") as attribute value delimiters and Ascii
apostrophes inside the value (as JavaScript string literal delimiters),
or vice versa. E.g. "this.innerHTML = 'X';" or 'this.innerHTML = "X";'

2) Write the Ascii apostrophes inside the value using the reference
&apos;, e.g. 'this.innerHTML = &apos;X&apos;;'

--
Yucca, http://www.cs.tut.fi/...

Jukka K. Korpela

12/28/2014 7:13:00 PM

0

2014-12-28, 20:46, Denis McMahon kirjoitti:

> You're using single quotes (') to delimit a string inside a string
> delimited with single quotes.

Yes, thatâ??s the problem here.

> Use double quotes (") for one of the strings.

Thatâ??s one solution.

> (`) might also work (it does in my firefox at least).

Thatâ??s not advisable. It does not work in other browsers, and itâ??s not
described in any standard or reference. It seems to be a nonstandard
feature in Firefox that grave accent (â??backtickâ?, `) characters are
recognized as string literal delimiters. This might be understandable as
an attempt to deal with an assumed typing error (people often confuse
small characters like this with each other). But I cannot imagine any
context where it might be justifiable to utilize the feature.

--
Yucca, http://www.cs.tut.fi/...

Thomas 'PointedEars' Lahn

12/28/2014 7:13:00 PM

0

Denis McMahon wrote:

> On Sun, 28 Dec 2014 18:12:19 +0100, Haris Bogdanovich wrote:
>> <td onclick='this.innerHTML = 'X';'>--</td>
>>
>> Why it won't change to X when I click on it ?
>
> [â?¦] Use double quotes (") for one of the strings.


> (`) might also work (it does in my firefox at least).

If so, only because of error correction.

<http://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#before-attribute-value...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Jukka K. Korpela

12/28/2014 8:30:00 PM

0

2014-12-28, 21:13, Lahn wrote:

>> (`) might also work (it does in my firefox at least).
> If so, only because of error correction.
>
> <http://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#before-attribute-value...

Of course, it has nothing to do with HTML error correction. Anyone who
actually understands the elements of the basics of the fundamentals of
HTML parsing, instead of showing off and making pointless â??learnedâ?
lectures, knows that.

--
Yucca, http://www.cs.tut.fi/...

Thomas 'PointedEars' Lahn

12/28/2014 8:32:00 PM

0

Jukka K. Korpela wrote:

> 2014-12-28, 19:25, Lahn wrote:
>> Haris Bogdanovich wrote:
>>> <td onclick='this.innerHTML = 'X';'>--</td>
>>>
>>> Why it won't change to X when I click on it ?
>>
>> Possibility: Assuming error correction, the resulting script code is
>>
>> this.innerHTML = X;
>>
>> and the identifier â??Xâ? is not defined. (See the error console. See the
>> FAQ for details on debugging.)
>
> Apparently Lahn did not bother actually looking at the error console.

Apparently you have no idea that observations in one browser version means
nothing for all others, browsers and versions. Hence there is no certainty,
only â??possibilityâ?.

> Should we bother looking at this â??FAQâ?, which he calls â??the FAQâ??

You should bother looking at it.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Thomas 'PointedEars' Lahn

12/28/2014 8:44:00 PM

0

Jukka K. Korpela wrote:

> 2014-12-28, 21:13, Lahn wrote:
>>> (`) might also work (it does in my firefox at least).
>> If so, only because of error correction.
>>
>> <http://www.w3.org/TR/2014/REC-html5-20141028/syntax.html#befor... attribute-value-state>
>
> Of course, it has nothing to do with HTML error correction. Anyone who
> actually understands the elements of the basics of the fundamentals of
> HTML parsing, instead of showing off and making pointless â??learnedâ?
> lectures, knows that.

The HTML5 parsing algorithm at least, which I cited, states that if a
backtick (`) is parsed at this position, it constitutes a â??Parse errorâ?,
that should be treated like â??Anything elseâ?, therefore â??append[ed] [â?¦] to
the current attributeâ??s valueâ?. This means it would become part of the
attribute value.

An event-handler attribute value is parsed (per the definition of the
default scripting language) as an ECMAScript /Program/. But a backtick as
first character of an ECMAScript /Program/ is a syntax error in ECMAScript
implementations because it cannot be produced by production

| Program : SourceElements_opt

(assuming no syntax extensions). Therefore, error correction would be
necessary to execute that /Program/.

<http://ecma-international.org/ecma-262/5.1/#...

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Jukka K. Korpela

12/28/2014 9:50:00 PM

0

2014-12-28, 22:44, Lahn wrote:

> The HTML5 parsing algorithm at least, which I cited, states that if a
> backtick (`) is parsed at this position, it constitutes a â??Parse errorâ?,

The issue was the use of the code

<td onclick='this.innerHTML = `X`;'>--</td>

which was stated to work in Firefox. (If you interpreted the description
so that grave accents were used instead of outermost apostrophes, you
did not pay attention. Such a construct does not work in Firefox, or any
browser.)

There is no HTML error in that code. The error is in JavaScript code.
JavaScript is a programming language discussed in this group, thought
the resident troll regularly denies its existence.

The HTML attribute value is parsed without problems. The acute accent is
just another character there.

--
Yucca, http://www.cs.tut.fi/...

Jukka K. Korpela

12/28/2014 9:57:00 PM

0

2014-12-28, 22:32, Lahn wrote:

> Jukka K. Korpela wrote:
>
>> 2014-12-28, 19:25, Lahn wrote:
>>> Haris Bogdanovich wrote:
>>>> <td onclick='this.innerHTML = 'X';'>--</td>
>>>>
>>>> Why it won't change to X when I click on it ?
>>>
>>> Possibility: Assuming error correction, the resulting script code is
>>>
>>> this.innerHTML = X;
>>>
>>> and the identifier â??Xâ? is not defined. (See the error console. See the
>>> FAQ for details on debugging.)
>>
>> Apparently Lahn did not bother actually looking at the error console.
>
> Apparently you have no idea that observations in one browser version means
> nothing for all others, browsers and versions. Hence there is no certainty,
> only â??possibilityâ?.

If you actually knew HTML parsing rules instead of giving long lectures,
you would know that your â??possibleâ? error correction cannot happen.
There is no error in the first attribute specification, so it would be
grossly inappropriate to â??error correctâ? its value to something
different. The second apostrophe terminates the attribute specification.

--
Yucca, http://www.cs.tut.fi/...