JavaScript Where To Write JavaScript Code
Where to write JavaScript code?
JavaScript code can be embedded within the HTML page or can be written in an external file.
Depending on where we write JavaScript code, there are 3 ways of writing JavaScript code.
We will be talking about inline scripting later in this course.
Here we will talk about internal and external scripting.
Internal script
When lines of JavaScript code are written within the HTML file itself, it is called internal scripting.
Internal scripting, is done with the help of HTML tag htmlspecialchars()
This tag can be placed either in the head tag or body tag within the HTML file. The significance of its placement will be discussed later when we learn about the DOM API.
Within our HTML page, we can write JavaScript code within this tag.
External script
JavaScript code can be written in an external file also. The file containing JavaScript code is saved with the extension *.js(e.g. fileName.js)
To include the external JavaScript file we will use the script tag in HTML with attribute 'src'.
Remember, in external file, JavaScript code is not written inside htmlspecialchars(<script> </script>) tag.
Internal v/s External script
The below-mentioned points can help you choose between these two ways of writing the script based on some parameters.
Note : Throughout this course, we will use external scripting. But due to the platform built-in feature, we will not explicitly link HTML code to external script using htmlspecialchars() tag. HTML code that we write is automatically linked to JavaScript code written on that page.