JavaScript How to
The HTML <script>
tag is used to insert a JavaScript into an HTML page.
Writing to the HTML Document
The example below writes a <p>
element with current date information to the HTML document by using (i) the method write
of the object document
and (ii) the date
function/object, which gives the current time.
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 type="text/javascript">
and
</script>
tells where the JavaScript starts & ends and are executed by the browser.
Changing HTML Elements
The example below writes the current date into an existing
<p>
element.
To manipulate HTML elements, the example uses
- the method
getElementById
of the object document
and
- the property
innerHTML
, which defines both the HTML code and the text that occurs between that element’s opening and closing tag.
Each HTML element has an innerHTML
property.
Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.