f



<script type="text/javascript" charset="utf-8"> // <!-- ?

Hi,

I've seen some pages that comment inlined <scripts> in this way:

<script type="text/javascript" charset="utf-8">
// <!--

 .... Here goes the script src

// -->
</script>

The reasoning being that it avoids the need to escape any tags inside
the script. But it's not very common. Why ? Any thoughts ?
--
Jorge.
0
Ry
10/9/2010 9:32:51 PM
comp.lang.javascript 38370 articles. 0 followers. javascript4 (1315) is leader. Post Follow

9 Replies
1047 Views

Similar Articles

[PageSpeed] 20

Ry Nohryb wrote:

> I've seen some pages that comment inlined <scripts> in this way:
>
> <script type="text/javascript" charset="utf-8">
> // <!--

It's codelore from the previous millennium. It was relevant when Netscape 2 
ruled the Earth, or something like that.

> The reasoning being that it avoids the need to escape any tags inside
> the script.

That's fairly absurd.

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

0
Jukka
10/9/2010 10:02:34 PM
On Oct 10, 12:02=A0am, "Jukka K. Korpela" <jkorp...@cs.tut.fi> wrote:
> Ry Nohryb wrote:
> > I've seen some pages that comment inlined <scripts> in this way:
>
> > <script type=3D"text/javascript" charset=3D"utf-8">
> > // <!--
>
> It's codelore from the previous millennium. It was relevant when Netscape=
 2
> ruled the Earth, or something like that.
>
> > The reasoning being that it avoids the need to escape any tags inside
> > the script.
>
> That's fairly absurd.

Is it ?
There's no need any more to escape </script> in inlined scripts ?
--
Jorge.
0
Ry
10/9/2010 11:04:35 PM
On 10/09/2010 07:04 PM, Ry Nohryb wrote:
> On Oct 10, 12:02 am, "Jukka K. Korpela"<jkorp...@cs.tut.fi>  wrote:
>> Ry Nohryb wrote:
>>> I've seen some pages that comment inlined<scripts>  in this way:
>>
>>> <script type="text/javascript" charset="utf-8">
>>> //<!--
>>
>> It's codelore from the previous millennium. It was relevant when Netscape 2
>> ruled the Earth, or something like that.
>>
>>> The reasoning being that it avoids the need to escape any tags inside
>>> the script.
>>
>> That's fairly absurd.
>
> Is it ?
> There's no need any more to escape</script>  in inlined scripts ?

You would only need to use <![CDATA[ and ]]> in case you are delivering 
your HTML as xHTML.

I always put them. in all cases.
0
Marious
10/9/2010 11:31:48 PM
Ry Nohryb wrote:

> On Oct 10, 12:02 am, "Jukka K. Korpela" <jkorp...@cs.tut.fi> wrote:
>> Ry Nohryb wrote:
>>> I've seen some pages that comment inlined <scripts> in this way:
>>
>>> <script type="text/javascript" charset="utf-8">
>>> // <!--
>>
>> It's codelore from the previous millennium. It was relevant when
>> Netscape 2 ruled the Earth, or something like that.
>>
>>> The reasoning being that it avoids the need to escape any tags
>>> inside the script.
>>
>> That's fairly absurd.
>
> Is it ?
> There's no need any more to escape </script> in inlined scripts ?

Are you serious?

The use of // <!-- has no effect whatsoever on such matters. Try it. It's 
very simple. You will see that whether you use // <!-- or not, the string 
</script> terminates the script element, so of course if you wish to write 
e.g. the string '</script>' inside code inside a script element, you need 
use some technique to prevent the end that construct from appearing there, 
e.g. by writing '<'+'/script'.

Besides, // <!-- would be wrong codelore even if we considered Netscape 2. A 
browser that does not recognize the <script> tag (and this is what the 
"magic" really related to) works as if the <script> and </script> tag were 
not there, processing the content between them. The old trick of starting 
script element content with <!-- // made that content appear as an HTML 
comment to the ancient browsers. But using // <!-- makes the characters "//" 
precede that "comment", so Netscape 2 would display them as document 
content.

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

0
Jukka
10/10/2010 11:32:03 AM
On Oct 10, 1:32=A0pm, "Jukka K. Korpela" <jkorp...@cs.tut.fi> wrote:
> Ry Nohryb wrote:
> > On Oct 10, 12:02 am, "Jukka K. Korpela" <jkorp...@cs.tut.fi> wrote:
> >> Ry Nohryb wrote:
> >>> I've seen some pages that comment inlined <scripts> in this way:
>
> >>> <script type=3D"text/javascript" charset=3D"utf-8">
> >>> // <!--
>
> >> It's codelore from the previous millennium. It was relevant when
> >> Netscape 2 ruled the Earth, or something like that.
>
> >>> The reasoning being that it avoids the need to escape any tags
> >>> inside the script.
>
> >> That's fairly absurd.
>
> > Is it ?
> > There's no need any more to escape </script> in inlined scripts ?
>
> Are you serious?
>
> The use of // <!-- has no effect whatsoever on such matters. Try it. It's
> very simple. You will see that whether you use // <!-- or not, the string
> </script> terminates the script element, so of course if you wish to writ=
e
> e.g. the string '</script>' inside code inside a script element, you need
> use some technique to prevent the end that construct from appearing there=
,
> e.g. by writing '<'+'/script'.
>
> Besides, // <!-- would be wrong codelore even if we considered Netscape 2=
.. A
> browser that does not recognize the <script> tag (and this is what the
> "magic" really related to) works as if the <script> and </script> tag wer=
e
> not there, processing the content between them. The old trick of starting
> script element content with <!-- // made that content appear as an HTML
> comment to the ancient browsers. But using // <!-- makes the characters "=
//"
> precede that "comment", so Netscape 2 would display them as document
> content.

Ok. So // <!-- won't work for html, but // <![CDATA[ would do the
trick for xhtml, is that right ?
Thanks,
--
Jorge.
0
Ry
10/10/2010 9:19:06 PM
Ry Nohryb wrote:

> So // <!-- won't work for html, but // <![CDATA[ would do the
> trick for xhtml, is that right ?

Yes, as described at
http://www.w3.org/TR/xhtml1/#h-4.8

But why would you use XHTML? People who want to use the most modern stuff no 
matter what are generally moving to HTML 5 these days.

Besides, using an external script avoids the problem nicely.

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

0
Jukka
10/11/2010 3:42:07 PM
On Oct 11, 5:42=A0pm, "Jukka K. Korpela" <jkorp...@cs.tut.fi> wrote:
> Ry Nohryb wrote:
> > So // <!-- won't work for html, but // <![CDATA[ would do the
> > trick for xhtml, is that right ?
>
> Yes, as described athttp://www.w3.org/TR/xhtml1/#h-4.8
>
> But why would you use XHTML? People who want to use the most modern stuff=
 no
> matter what are generally moving to HTML 5 these days.
>
> Besides, using an external script avoids the problem nicely.

Ok. Thanks.
--
Jorge.
0
Ry
10/11/2010 3:56:00 PM
In comp.lang.javascript message <fvhso.4354$7N7.2568@uutiset.elisa.fi>,
Sun, 10 Oct 2010 14:32:03, Jukka K. Korpela <jkorpela@cs.tut.fi> posted:

>The use of // <!-- has no effect whatsoever on such matters. Try it.
>It's very simple. You will see that whether you use // <!-- or not, the
>string </script> terminates the script element, so of course if you
>wish to write e.g. the string '</script>' inside code inside a script
>element, you need use some technique to prevent the end that construct
>from appearing there, e.g. by writing '<'+'/script'.


You should not assume that the code for a Web page will be read only by
a Web browser.  I have a testing tool for potential Web pages which can
fail on certain JavaScript if that script is not made into an HTML
comment; and the author does not intend to change his code, since he
does not currently program in the tool's language.  Since I call the
tool from a DOS batch file which also does other useful things, I'll
continue to use the tool because the backup tool has to be run manually.

When wandering from one's customary haunts into another newsgroup, one
should try not to appear to recommend inferior code.  One need only
write '<\/script' which is shorter and more readable.

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike v6.05  IE 8.
 Web  <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
 Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
 DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
0
Dr
10/11/2010 7:01:27 PM
Dr J R Stockton wrote:

> You should not assume that the code for a Web page will be read only
> by a Web browser.

Who is assuming such things?

> I have a testing tool for potential Web pages
> which can fail on certain JavaScript if that script is not made into
> an HTML comment; and the author does not intend to change his code,

That's an odd tool. Does it also require that the string "42" must occur 
inside JavaScript code, or else it explodes? That would be equally absurd. 
But you would not flame a casual Usenaut who says that one does not need to 
use "42" inside every script, would you?

Besides, starting something with // <!-- or <!-- // and ending it with 
// --> inside a script element does _not_ constitute an HTML comment in HTML 
4.01 and relatives - it's just data inside the element. And in XHTML, it 
_is_ a comment, and that means that it's something that a conforming user 
agent may silently remove before otherwise processing the document - i.e., 
it may throw away the script.

> When wandering from one's customary haunts into another newsgroup, one
> should try not to appear to recommend inferior code.

Are you trying hard to be sarcastic?

> One need only
> write '<\/script' which is shorter and more readable.

Whether you use this or that method of "escaping" is immaterial here, and my 
example '<'+'/script' is at least equally good as yours. If you think saving 
one character is relevant here, please have you reality sensors calibrated. 
In clarity, '<'+'/script' is based on using the plus sign for string 
catenation, a rather basic and commonly known feature, whereas '<\/script' 
requires specific knowledge about the use of the reverse solidus as a 
character that may be used as a dummy before any character for no particular 
reason - technically, to "kill" the role of a character as a special 
character even if the character is _not_ a special character -  as far as 
JavaScript as such is concerned.

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

0
Jukka
10/12/2010 4:31:08 PM
Reply: