How to write Javascript into HTML code

To insert a JavaScript into an HTML page, use the <script> tag.
Inside the <script> tag use the type attribute to define the scripting language.
The <script> and </script> tells where the JavaScript starts and ends:

<html>
<body>
<h1>My First Web Page</h1>

<p id=”demo”>This is a paragraph.</p>

<script type=”text/javascript”>
… Your JavaScript code Here …
</script>

</body>
</html>

Try this example below (This will display date) :

<html>
<body>
<h1>My First Web Page</h1>

<p id=”demo”>This is a paragraph.</p>

<script type=”text/javascript”>
document.getElementById(“demo”).innerHTML=Date();
</script>

</body>
</html>

Some Browsers do Not Support JavaScript

Browsers that do not support JavaScript, will display JavaScript as page content. To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to “hide” the JavaScript.
Just add an HTML comment tag <!– before the first JavaScript statement, and a –> (end of comment) after the last JavaScript statement.

Example :

<script type=”text/javascript”>
<!–
document.getElementById(“demo”).innerHTML=Date();
//–>
</script>

  • Share/Save/Bookmark