# JavaScript Where TO...
The <Script> Tag
In HTML, JavaScript code must be inserted between <script> & </script><Used Atom editor> |
JavaScript in <head> or <body>
I can Place any number of scripts in an HTML document. Scripts can be placed in the <body>, or in the <head> section of an HTML page, or in both.
JavaScript in <head>
In below example, a JavaScript function is placed in the <head> section of an HTML Page. The function is invoked when a button clicked:
<Example Code> |
Result is as follows.
<Before Click> |
<After Click> |
JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an HTML Page. The function is invoked when a button is clicked:
<Example Code> |
Result is as follows:
<Before Click> |
<After Click> |
External JavaScript
Scripts can also be placed in external files:
- It separates HTML and code
- It makes HTML and JavaScript easier to read and maintain
- Cached JavaScript files can speed up page loads
External References
External Scripts can be referenced with a full URL or with a path relative to the current web page.
This example uses a full URL to link to a script:
This example uses a script located in a specified folder on the current web site:
This example links to a script located in the same folder as the current page:
No comments:
Post a Comment