HTML JavaScript
JavaScript makes HTML pages more dynamic and interactive.
The HTML <script> Tag
The HTML <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.
Example: Changing Content
This example writes "Date()" into an HTML element with id="demo".
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>Example: Changing Styles
This example changes the font size of an element.
<p id="demo">A Paragraph.</p>
<button type="button" onclick="document.getElementById('demo').style.fontSize='35px'">Click Me</button>Example: Changing Image Sources
JavaScript can change HTML attribute values, like the `src` of an image.
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif'">Turn on the light</button>
<img id="myImage" src="pic_bulboff.gif" style="width:100px">
<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>The <noscript> Tag
The HTML <noscript> tag defines an alternate content to be displayed to users that have disabled scripts in their browser or have a browser that doesn’t support script.
<noscript>Sorry, your browser does not support JavaScript!</noscript>Test Yourself with an Exercise
Which HTML tag is used to contain JavaScript code?