Where JavaScript to be added in code

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.

How to add JavaScript to code to page

There are 3 ways to add a JavaScript tag or code to HTML and other pages.

  • Embedding code: 

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:   

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>
  • External file

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!");
}

Advantages Of NCERT, CBSE & State Boards Solutions For All Subjects

  • All the NCERT Solutions have been prepared by academic experts having 10+ years of teaching experience.
  • They have prepared all the solutions in simple and easy language, so that each and every student can understand the concepts easily.
  • All the solutions have been explained step to step wise in details with better explainations.
  • Students can also use these question and answers for your assignments and in homework help.
  • All the solutions have been explained in detail and the answers have been compiled in a step-wise manner.
  • All the question and asnwers are commonly prepared according to the Latest Syllabus of Board Education and Guidelines.
  • Students can know about the various types of questions asked in the exams with the help of these solutions.

Top Engineering Courses