Introduction, History and features of JS
Javascript is a frontend technology used to embed user interactivity into webpage. It makes the webpage dynamic. It is not a programming language but scripting language.
JavaScript was invented by Brendan Eich in 1995. JavaScript and Java are completely different languages, both in concept and design.
We can check the output of javascript through browser by embedding inside html code. We cannot run javascript directly to check output like programming languages. To do that, we have to install node.js in our system.
JS Insertion methods into HTML file
JavaScript can be inserted using <script> element in between <script> and </script> tags. This element can appear inside head element or body element.
There are 3 places to insert java script :
- Inside head element
- Inside body element
- As external file
JavaScript inside head element
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById(“demo”).innerHTML = “Paragraph changed.”;
}
</script>
</head>
<body>
<h2>Demo JavaScript in Head</h2>
<p id=”demo”>A Paragraph.</p>
<button type=”button” onclick=”myFunction()”>Try it</button>
</body>
</html>
Javascript inside body element
<!DOCTYPE html>
<html>
<body>
<h2>Demo JavaScript in Body</h2>
<p id=”demo”>A Paragraph.</p>
<button type=”button” onclick=”myFunction()”>Try it</button>
<script>
function myFunction() {
document.getElementById(“demo”).innerHTML = “Paragraph changed.”;
}
</script>
</body>
</html>
Javascript as external file
External file: myScript.js
function myFunction() {
document.getElementById(“demo”).innerHTML = “Paragraph changed.”;
}
<!DOCTYPE html>
<html>
<body>
<h2>Demo External JavaScript</h2>
<p id=”demo”>A Paragraph.</p>
<button type=”button” onclick=”myFunction()”>Try it</button>
<p>This example links to “myScript.js”.</p>
<p>(myFunction is stored in “myScript.js”)</p>
<script src=”myScript.js”></script>
</body>
</html>