The example below manipulate the content of an existing <p> element when the page loads:
The example below calls a function in the <head> section when a button is clicked:
This example also calls a function when a button is clicked, but the function is in the <body>:
Scripts in <head> and <body>
You can place an unlimited number of scripts in your document, and you can have scripts in both the <body> and the <head> section at the same time.
It is a common practice to put functions in the <head> section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.
Scripts can also be placed in external files. External files often contain code to be used by several different web pages.
To use an external script, point to the .js file in the "src" attribute of the <script> tag:
You can place the script in the <head> or <body> as you like. The script will behave as it was located exactly where you put the <script> tag in the document.
|External scripts cannot contain <script> tags.|