Closed Bug 48962 Opened 24 years ago Closed 24 years ago

JavaScript functions declared in comments don't work

Categories

(Core :: JavaScript Engine, defect, P3)

x86
Windows NT
defect

Tracking

()

VERIFIED DUPLICATE of bug 31255

People

(Reporter: john, Assigned: rogerl)

References

()

Details

Attachments

(1 file)

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.
Attached file The testcase.
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.
what about if you close the html comment with a leading javascript comment:
//-->
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.
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
Closed: 24 years ago
Resolution: --- → DUPLICATE
Marking Verified -
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: