How to add javaScript file in html file

Cannot use javaScript file without adding html file. There are two ways to write javaScript code -

1 External

2 internal

1 External javascript

Write javaScript code inside javaScript file. On saving it, a .js extension with file name is given. In html file, when creating a separate file to apply javaScript to an html document file, it is called external javaScript. Write the code in the file and save the file by giving it the .js extension along with the file name. You cannot use external javaScript file without linking the .js file to html file, so it is necessary to link it.

Link this .js file using script tag, place it inside the <head> tag, use two attributes, type and src in script tag. Its value in type attribute is given text / javascript if html and javaScript If the file is inside a separate disk / folder then the value of the src attribute needs to be given the path or location with the name of the .js file if the html and javaScript file is inside a folder then the name extension of the .js file is Also, with the help of this value that the javaScript file is linked, put the script tag inside the head tag.

<script type = “text / javascript” src =”fileName.js” ></script>

Note: - Keep both the beginner student html and javascript file in the same folder.

Example 1 : Index.html

<!DOCTYPE html>

<html>

<head>

<title></title>

<script type="text/javascript" src="fileName.js"></script>

</head>

<body>

</body>

</html>

Example 1.2 : fileName.js

document.write("JavaScript External js example");

Output

JavaScript External js example

2 Internal javascript

The code of javaScript is written inside the script tag in html file, it is called internal js. In order to apply javaScript to an html document file no separate file is created, whichever html file you have to apply javaScript, the code for that file is written between the <script> and </script> tag within the same file. Its value text / javascript is given in the type attribute with the script tag, internal javaScript can be written inside the head or body tag. This one html file contains both html and javaScript code so there is no need to link any file in it. is .

Example : <script type = “text/javascript”> …. </script>

Example 2

<!DOCTYPE html>

<html>

<head>

<title></title>

<script type="text/javascript">

document.write("JavaScript internal js example");

</script>

</head>

<body>

</body>

</html>

Output

JavaScript internal js example

How to use Internal and External js file together ? 

Internal and external javaScript code can be applied to html file simultaneously, for this, we will use two script tags, first we will link the external js file under the script tag and write the code of javaScript inside the second script tag.

Internal javaScript code can be used in a single html file while linking the code of external javaScript to more than one html file can be used so that one code is reused in multiple html file so that one code can be written again and again. It is not necessary.

Example

<script type = “text / javascript” src =”fileName.js” />

<script type = “text/javascript”>…… </script>

Software required

To learn javaScript, use code editor and browser, code editor use to write code and browser is used to see its output.

1 code editor: - notepad, notepad ++, sublime, bracket, adobe dreamweaver etc .. You can use one of these code editor.

2 Web Browser: - google chrome, safari, mozila firefox, opera etc .. You can use one of these browsers.


Connect with us

facebook logo
Email : contact@amcodestar.com
© Copyright amcodestar.com 2020 - 2023 All Rights Reserved.
About us     term and condition      comment policy
DMCA.com Protection Status