HTML JavaScript

<script> Tag

The <script> tag is used to define a client-side script (JavaScript).

The <script> element either contains scripting statements, or it points to an external script file through the src attribute.

Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

To select an HTML element, JavaScript very often use the document.getElementById(id) method.

HTML Code:

<p id="demo01"></p> <script> document.getElementById("demo01").innerHTML = "Hello JavaScript!"; </script>

Result:


Example 01

HTML Code:

<h1>My First JavaScript</h1> <p>JavaScript can change the content of an HTML element:</p> <button type="button" onclick="myFunction01()">Click Me!</button> <p id="demo02">This is a demonstration.</p> <script> function myFunction01() { document.getElementById("demo02").innerHTML = "Hello JavaScript!"; } </script>

Result:

My First JavaScript

JavaScript can change the content of an HTML element:

This is a demonstration.


Example 02

HTML Code:

<h1>My First JavaScript</h1> <p id="demo03">JavaScript can change the style of an HTML element.</p> <script> function myFunction02() { document.getElementById("demo03").style.fontSize = "25px"; document.getElementById("demo03").style.color = "red"; document.getElementById("demo03").style.backgroundColor = "yellow"; } </script> <button type="button" onclick="myFunction02()">Click Me!</button>

Result:

My First JavaScript

JavaScript can change the style of an HTML element.


<noscript> Tag

The <noscript> tag is used to provide an alternate content for users that have disabled scripts in their browser or have a browser that doesn't support client-side scripts.

HTML Code:

<p id="demo04"></p> <script> document.getElementById("demo04").innerHTML = "Hello JavaScript!"; </script> <noscript>Sorry, your browser does not support JavaScript!</noscript> <p>A browser without support for JavaScript will show the text written inside the noscript element.</p>

Result:

A browser without support for JavaScript will show the text written inside the noscript element.

HTML Index

References

  1. W3Schools

This page is only a summary of the material from W3Schools. Definitions, Examples and Codes are exclusive property of W3Schools.