最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is this Javascript printing my comments in the rendered HTML - Stack Overflow

programmeradmin0浏览0评论

I'm new to Javascript, as in just really getting started with it today. I'm testing some very basic code in the first chapter of a book and encountered a problem on the first example. I wrote the code in Notepad++, but even in that program my second line of menting is black and not green. Why does the line right after </noscript> render?

The output to my browser is rendered as: Hello World! // Display a message dialog after the page has loaded.

<!DOCTYPE html>
<html>
<body>
    <div id = "panel">
    <script type = "text/javascript">
    // Dynamically write a text string as the page loads.
    document.write("Hello World!");
    </script>
    <noscript>Javascript is Not Enabled!</noscript>
    // Display a message dialog after the page has loaded.
    <body onload = " window.alert('Document Loaded!');">
    </div>
</body>
</html>

I'm new to Javascript, as in just really getting started with it today. I'm testing some very basic code in the first chapter of a book and encountered a problem on the first example. I wrote the code in Notepad++, but even in that program my second line of menting is black and not green. Why does the line right after </noscript> render?

The output to my browser is rendered as: Hello World! // Display a message dialog after the page has loaded.

<!DOCTYPE html>
<html>
<body>
    <div id = "panel">
    <script type = "text/javascript">
    // Dynamically write a text string as the page loads.
    document.write("Hello World!");
    </script>
    <noscript>Javascript is Not Enabled!</noscript>
    // Display a message dialog after the page has loaded.
    <body onload = " window.alert('Document Loaded!');">
    </div>
</body>
</html>
Share Improve this question edited May 29, 2013 at 17:18 Derek Henderson 9,6964 gold badges45 silver badges71 bronze badges asked May 29, 2013 at 17:03 Vin BreauVin Breau 2793 silver badges18 bronze badges 4
  • 2 You also have two BODY tags in your HTML. – 2C-B Commented May 29, 2013 at 17:06
  • // Display a message dialog after the page has loaded. needs to be inside your script tag. You've got it outside – dewd Commented May 29, 2013 at 17:06
  • Essentially, anywhere you want to include a JS ment (//), place it inside your <script></script> tags. – AlbertEngelB Commented May 29, 2013 at 17:07
  • I found downloadable code samples from the author's website. When I pared his to what he described in the book, I saw I was vastly off. I chalk it up to his books' poor presentation. What kind of language book never shows full code block examples? – Vin Breau Commented May 29, 2013 at 18:28
Add a ment  | 

4 Answers 4

Reset to default 8

That's because you're writing your ment not in a JavaScript part but in an HTML one.

Comments in HTML are like this :

<noscript>Javascript is Not Enabled!</noscript> 
<!-- Display a message dialog after the page has loaded. -->

Note that you've put a body element inside the body, that's not good. You probably wanted this instead of the second body :

<script>
     window.onload = function(){
          alert('document loaded!');
     };
</script>

<!-- This is a ment in HTML --> /* This is a ment in Javascript */

The reason the line you tried to ment out is rendered is because you have attempted to ment out the text with JavaScript ments.

The browser rendering html sees the two slashes (//) as part of the text, not as markup designating a ment.

The correct way to ment out something in html is with <!-- and -->.

Your ment are not between script tag. You can move it into an script tag or use the HTML ment just like @22kar said. Also you need to put the parameter "onload" in the first body tag and remove the other one.

发布评论

评论列表(0)

  1. 暂无评论