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.

JS Ways of writing

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.

JS Internal Syntax

Within our HTML page, we can write JavaScript code within this tag.

JS Internal

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'.

JS External Syntax
JS External

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.

JS Comparison
Notepad

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.

#Where_to_write_JavaScript_code #Where_to_write_js_code #where_to_write_javascript_code_in_html #where_we_write_javascript_code #where_do_i_write_javascript_code #where_can_i_write_javascript_code

(New page will open, for Comment)

Not yet commented...