Wad Exp-2
Wad Exp-2
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>
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">
async: It is a boolean attribute. When present, it specifies that the script willbe
executed asynchronously when it is available.
<script async>
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");
}
<body>
<center>
<button onclick="showAlert()">Click for Alert</button>
</center>
</body>
External.html
Example3:
Program code
<DOCTYPE html>
<html>
<head>
</head>
<body>
</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>
</head>
<body>
<br>
<br>