JavaScript (“JS”) is an object-based scripting language whose interpreter is embedded inside web browser software such as Google Chrome, Microsoft Edge, Firefox, Opera, and Safari.
This allows scripts contained in a web page to be interpreted when the page is loaded in the browser to provide functionality. For security reasons, JavaScript cannot read or write files, with the exception of “cookie” files that store minimal data.
Before the introduction of JavaScript, web page functionality required the browser to call upon “server-side” scripts, resident on the web server, where slow response could impede performance. Calling upon “client-side” scripts resident on the user’s system, overcame the latency problem and provided a superior experience.
Including JavaScript in an HTML document
To include JavaScript code directly in an HTML document it must be inserted between <script> and </script> tags, like this:
<script>
document.getElementById( ‘message’ ).innerText = ‘Hello World!’
</script>
An HTML document can include multiple scripts, and these may be placed in the head or body section of the document. It is, however, recommended that you place scripts at the end of the body section (immediately before the </body> closing tag) so the browser can render the web page before interpreting the script.
JavaScript code can also be written in external plain text files that are given a .js file extension. This allows several different web pages to call upon the same script. In order to include an external script in the HTML document, the file name of the script must be assigned to a src attribute of the <script> tag, like this:
<script src=”external_script.js”> </script>
Again, this can be placed in the head or body section of the document, and the browser will treat the script as though it was written directly at that position in the HTML document.
Assigning only the file name of an external script to the src attribute of a <script> tag requires the script file to be located in the same folder (directory) as the HTML document. If the script is located in an adjacent folder you can assign the relative path address of the file instead, like this:
<script src=”js/external_script.js”> </script>
If the script is located elsewhere, you can assign the absolute path address of the file, like this:
<script src=”https://www.example.com/js/external_script.js”>
</script>
You can also specify content that will only appear in the web page if the user has disabled JavaScript in their web browser by including a <noscript> element in the body of the HTML document, like this:
<noscript>JavaScript is Not Enabled!</noscript>
Find out more about JavaScript and how to use it with:
JavaScript in easy steps, 6th edition – available now from our online shop in ebook and paperback formats.
£6.99 ebook / £11.99 paperback
By: Mike McGrath
ISBN: 9781840788778
Coding with HTML & JavaScript – Create Epic Computer Games – available now from our online shop in ebook and paperback formats
By: Max Wainewright
ISBN: 9781840789553