×

Html & Html5

The World's best Software Development Study Portal

Using JS in HTML


Actually we are having two methods by which we can use our JS inside the HTML. These are as follows :-

  • External Javascript
  • Internal Javascript

External Javascript

It means that you are having any different file in which you are writing the codes of Javascript language and this different file is linked with your existing webpage. And this link we provide through <script> tag inside the <head> tag. or a <body>

For Example:-

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="main.js"> </script>

</head>

<body>

<p> Let's see here </p>

<p id="main"> I am Manish Singh Bhakuni </p>

</body>

</html>

This stylesheet can be written in any editor but it must be saved with the .js extension.
Here is our "main.js" file . Let's see:-

document.write("Welcome to JS");

Output of the program

Welcome to JS

Internal style sheet

Internal javascript can be used with the same HTML page inside the <head> tag or <body> tag inside the <script> tag which must be inside the <head> tag.
For example:-

<!DOCTYPE html>

<html>

<head>

<script>

     document.write("Welcome to JS");

</script>

</head>

<body>

<p> Let's see here </p>

<p id="main"> I am Manish Singh Bhakuni </p>

</body>

</html>

Output of the program

Welcome to JS

So this is how we insert JS with our main page.






JS training insitute | Best IT Training classes in Gurgaon | Web Designing Training Institute