JavaScript functions declared in comments don't work

VERIFIED DUPLICATE of bug 31255

Status

()

P3
normal
VERIFIED DUPLICATE of bug 31255
18 years ago
17 years ago

People

(Reporter: john, Assigned: rogerl)

Tracking

Trunk
x86
Windows NT
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(URL)

Attachments

(1 attachment)

(Reporter)

Description

18 years ago
User-Agent: Mozilla/5.0 (Windows; U; WinNT4.0; en-US; m18) Gecko/20000814
BuildID:    2000081404

When you declare a JavaScript function in script comments, like so:
<SCRIPT>
<!--
function x() { }
-->
</SCRIPT>

Then the function will not be accessible.  If you drop the comments (<!-- and
-->) then the function becomes accessible.

Strangely, JavaScript inside comments appears to actually be interpreted, so
it's probably not entirely the parser's fault.  For example, if you place this
inside the head:
<SCRIPT>
<!--
alert("Yay");
-->
</SCRIPT>

Then the alert occurs.  It is only function definitions that seem to be affected.
(Reporter)

Comment 1

18 years ago
Created attachment 12898 [details]
The testcase.
(Reporter)

Comment 2

18 years ago
I wanted to note how the testcase worked: "Try Y" works because it is not in
comments, "Try X" does not work because it is.

Comment 3

18 years ago
what about if you close the html comment with a leading javascript comment:
//-->
(Reporter)

Comment 4

18 years ago
Thanks! That works great!  Now it is narrowed down to the end comment being the
culprit: still, only function declarations do not happen when this occurs.
alerts and such work fine.

More info: when functions are combined with an alert() in the block, the alert
does not happen.  The order of the two does not matter (alert vs. functions or
functions vs. alert).

IE5 and Netscape 4.73 work fine (the testcase doesn't work on Netscape without
tweaking, but the comment thing does) and I know for a fact that during the M17
cycle this worked fine in Mozilla too.  It is definitely a recent change.

Comment 5

18 years ago
John, thank you for reporting this; you are absolutely correct. 
I have to mark this one as a duplicate of bug 31255, though ...

This is a long story, but you will notice the most recent attachment
to bug 31255 is the same testcase that you have discovered. 

To see it quickly, you can use this URL in NN4.7: 
view-source:http://bugzilla.mozilla.org/showattachment.cgi?attach_id=7675

 

*** This bug has been marked as a duplicate of 31255 ***
Status: UNCONFIRMED → RESOLVED
Last Resolved: 18 years ago
Resolution: --- → DUPLICATE

Comment 6

18 years ago
Marking Verified -
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.