FSD-EXP-6(Applying JavaScript - internal and external, IO, Type Conversion)
FSD-EXP-6(Applying JavaScript - internal and external, IO, Type Conversion)
JavaScript Introduction
What is JavaScript?
JavaScript is the programming language of the web.
It can update and change both HTML and CSS.
It can calculate, manipulate and validate data.
JavaScript Where To
The <script> Tag
Internal and External JavaScript are the two ways of adding JavaScript code to an
HTML document. External JavaScript refers to adding JavaScript code in HTML
from a separate .js file using the src attribute of <script> tag while the Internal
JavaScript is the JavaScript code embedded within the script tag in the same
HTML document.
Internal JavaScript
Internal JavaScript refers to embedding JavaScript code directly within the
HTML file using <script> tag, either inside the <head> or <body> tag. This
method is useful for small scripts specific to a single page.
Syntax
<script>
// JavaScript code here
</script>
External JavaScript
External JavaScript is when the JavaScript code written in another file having an
extension .js is linked to the HMTL with the src attribute of script tag.
Syntax
<script src="url_of_js_file"> </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>
Internal JavaScript:
Example:
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
OutPut:
External JavaScript
//Path: C:\Users\Viswanath\Desktop\FSD\HTML\ss.js
// Filename: ss.js
let h2 = document.getElementById("demo");
h2.innerText = "This text is added by External JavaScript";
ExternalJavaScript.html
<html>
<body>
<h2 id="demo">Hello...</h2>
<script src="C:\Users\Viswanath\Desktop\FSD\HTML\ss.js"></script>
</body>
</html>
OutPut:
JavaScript Output
JavaScript Display Possibilities
Using innerHTML
The id attribute defines the HTML element. The innerHTML property defines the
HTML content:
Example:
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
OutPut:
Using document.write()
Example
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<p>Never call document.write after the document has finished loading.
It will overwrite the whole document.</p>
<script>
document.write(5 + 6);
</script>
</body>
</html>
OutPut:
<head>
<title>JavaScript Output</title>
</head>
<body>
<h2>
Display Output using window.alert() Method
</h2>