JavaScript, also known as JS, is one of the most popular client-side scripting languages, and widely used in web development to create interactive, dynamic, and web pages. In other words, we can say that
The term scripting is used for languages that require the support of another language to get executed. For example, JavaScript programs cannot get executed without the help of HTML or without integrated into HTML code.
There are 3 ways to add a JavaScript tag or code to HTML and other pages.
We can use the <script> </script> tag to add the JavaScript code into the HTML pages that wrap around JavaScript code inside the HTML program. In some cases, we also add the JavaScript code the body section <body> or head section <head>. The javascript programming language depends on the structure of the web page.
<!DOCTYPE html> <html> <head> <title> Welcome to Ekgurukul.com </title> <script> document.write("Welcome to open ekgurukul.com website to learn basics concept of javascript programming language"); </script> </head> <body> <p>Here we add javascript code in HEAD section of a page</p> </body> </html>
Inline code is used when we have to call a function in the HTML or add JavaScript code directly event attributes such as OnClick, OnMover, event listener, etc. We can add JavaScript directly in the HTML without using the <script> Add Your Code Here </script> tag.
<!DOCTYPE html > <html> <head> <title> Welcome to ekgurukul.com</title> </head> <body> <p> <a href="#" onClick="alert('Welcome to Universal Portal for Education :ekgurukul.com');">Click Here</a> </p> <p> This is the example to inline JavaScript or directly add to an HTML tag hyperlink. </p> </body> </html>
We can add an external (.js) extension file to hold the code of JavaScript. It is very helpful, where the same code used in multiple files. We include or attached this document with <script> tag with the src attribute. This minimizes your code to write the same code add it on multiple pages.
<html> <head> <meta charset="utf-8"> <title>Add a External JavaScript File in your document</title> </head> <body> <form> <input type="button" value="Result" onclick="print_Hello()"/> </form> <script src="print_hello.js"> </script> </body> </html>
In the print_hello.js file, we add some javascript code with function name 'print_Hello';
function print_Hello() { alert("Hello World!"); }