0% found this document useful (0 votes)
22 views5 pages

Wad Exp-2

excellent

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Wad Exp-2

excellent

Uploaded by

Keerthi bolimera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

Applying Javascript(Internal and External)

There are 2 ways to use the javascript in the HTML file:

Internal Javascript
Internal JavaScript :JavaScript can be added directly to the HTML file by writing the code
inside the <script> tag . We can place the <script> tag either inside <head>or the <body> tag
according to the need..
Example :
<html>
<head>
<title>Internal JavaScript</title>

<script type="text/javascript">
document.write("This is Internal Javascript Example.!!!");
</script>

</head>
<body>

</body>
</html>

Advantages of using Internal JS:


 Browser does not have to make an extra HTTP request for JavaScript code.
 It does not allow caching.

External Javascript
External Javascript : The other way is to write JavaScript code in another file havinga .js
extension and then link the file inside the <head> or <body> tag of the HTML file in which we
want to add this code.

In the HTML file, the <script> tag can also be used to indicate the location of a JavaScript file.
The src attribute is assigned the path and filename of the file.
For example:

The use of external JavaScript is more practical when the same code is to be used in
many different web pages.( It provides code re usability because single JavaScript file can be used
in several html pages.) Using an external script is easy , just put the name of the script
file(our .js file) in the src (source) attribute of <script>tag. External JavaScript file can
not contain <script> tags.
Syntax :
<script type="media_type" src="URL"> </script>

Attribute values:
 type: It is used to specify the MIME type of script and identify the content ofthe
Tag. It has a default value which is “text/javascript”.
 <script type="media_type">

 src: It is used to specify the URL of an external JavaScript file.


 <script src="URL"> </script>

 async: It is a boolean attribute. When present, it specifies that the script willbe
executed asynchronously when it is available.
 <script async>

 defer: It is a boolean attribute that is used to specify that script is executedwhen


the page has finished parsing.
 <script defer>

 integrity: It is used to give permission to the Browser to check the fetchedscript


to make ensure the source code is never loaded.
 <script integrity="filehash">

 referrerpolicy: It is used to specify the reference information that will be sentto


the server when fetching the script.
 Multiple script files can also be added to one page using several <script>tags.
 <script src="file1.js"> </script>
 <script src="file2.js"> >/script>
Advantages of External JavaScript
There will be following benefits if a user creates an external javascript:

1. It helps in the reusability of code in more than one HTML file.


2. It allows easy code readability.
3. It is time-efficient as web browsers cache the external js files, which furtherreduces
the page loading time.
4. It enables both web designers and coders to work with html and js files parallellyand
separately, i.e., without facing any code conflictions.
5. The length of the code reduces as only we need to specify the location of the jsfile.

external.js
document.write("This is External Javascript Example.!!!");
Example1 :
<html>
<head>
<title>External JavaScript</title>
<script type="text/javascript" src="external.js"></script>
</head>
<body>

Example2

function showAlert() {
alert("This Alert Is Written In External JavaScript");
}

For this example, the JavaScript file name is “script.js”.


<head>
<script type="text/JavaScript" src="script.js"></script>
<head>

<body>
<center>
<button onclick="showAlert()">Click for Alert</button>
</center>
</body>
External.html

Example3:

Program code
<DOCTYPE html>

<html>

<head>

<title>How to add External JavaScript File to HTML Page</title>

</head>

<body>

<script src = "myScript.js"></script> // myScript is the name of JavaScript file.

</body>

</html>

have to
specify the complete path of file (including drive and folder names)like this:
<head>

Output of code:

I love JavaScript
Programming!
Example4:
To connect multiple external javascript files to an HTML web page. JavaScript files
will display three messages on the browser. Look at theprogram code below:
Program code 2:
<DOCTYPE html>

<html>

<head>

<title>Adding Multiple External JavaScript Files to HTML Page</title>

</head>

<body>

<script src = "myScript.js"></script>

<br>

<script src = "myScript2.js"></script>

<br>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy