DigitalJedi
DigitalJedi

Reputation: 1651

javascript inline comment inside function call

So I am having this document:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function() {
                //executes when HTML-Document is loaded and DOM is ready
                alert("document is ready");
            });

            $(window).on("load", function () {
                //executes when HTML-Document is loaded and DOM is ready
                alert("window is loaded");
            });
        </script>
    </body>
</html>

Note the inline comments inside the 2 function calls.

Apparently these do NOT work and give me an error:

SyntaxError: missing } after function body[Weitere Informationen] index:1:297 note: { opened at line 1, column 37

However using a multiline comment /* */ is working just perfectly. I`m assuming, that the single line comments dont work becuase somehow the javascript gets minified into one line. Can sombody evaluate an how this is happening? Where can you use single line comments and where not? Or is it just generally a bad idea to use single line comments in js?

Upvotes: 0

Views: 1651

Answers (1)

Emily
Emily

Reputation: 201

Because after minifying everything is just one line, a single line comment, which doesn't have an end-of-comment tag, doesn't work.

Upvotes: 2

Related Questions