JavaScript is the most popular scripting language in the web to improve the design, validate forms, detect browsers, create cookies and many more client side functionalities and works in all major browsers, such as Internet Explorer, Mozilla Firefox, Google Chrome, Netscape, Opera and many more.
It is a lightweight programming language consisting of lines of executable computer code and usually embedded directly into HTML pages.
To write JavaScript with HTML page, you have to use the <script> tag along with the type attribute to define the scripting language. So the <script type="text/javascript"> and </script> tells where the javascript starts and end.
There are different ways to write JavaScript with HTML, which are as follows.
You can write <script> tag in the head section as follows.
<html>
<head>
<script type="text/javascript">
javascript codes are written here
</script>
</head>
<body>
</body>
</html>
You can write <script> tag in the body section as follows.
<html>
<head>
</head>
<body>
<script type="text/javascript">
javascript codes are written here
</script>
</body>
</html>
<html>
<head>
<script type="text/javascript">
It is a lightweight programming language consisting of lines of executable computer code and usually embedded directly into HTML pages.
To write JavaScript with HTML page, you have to use the <script> tag along with the type attribute to define the scripting language. So the <script type="text/javascript"> and </script> tells where the javascript starts and end.
Different Ways to Write JavaScript with HTML
There are different ways to write JavaScript with HTML, which are as follows.
Scripts in the HEAD Section
When scripts are placed in head section, they needs to be executed when they are called, or when an event is triggered and it will ensure that the script is loaded before anyone uses it.You can write <script> tag in the head section as follows.
<html>
<head>
<script type="text/javascript">
javascript codes are written here
</script>
</head>
<body>
</body>
</html>
Scripts in the BODY section
As in the head section, you can also place JavaScript codes in the body section using <script> tag and it will be executed when the page loads.You can write <script> tag in the body section as follows.
<html>
<head>
</head>
<body>
<script type="text/javascript">
javascript codes are written here
</script>
</body>
</html>
Scripts in both the HEAD and BODY section
You can write <script> tag in both the head and body section as follows.<html>
<head>
<script type="text/javascript">
javascript codes are written here
</script>
</head>
<body>
<script type="text/javascript">
javascript codes are written here
</script>
</body>
</html>
If you needs to write same JavaScript code on several pages, you can write it on separate external page with a .js file extension and can link it to the HTML file.
You can link external JavaScript file to HTML file as follows.
<html>
<head>
<script src="javascript.js">
</script>
</head>
<body>
</body>
</html>
</script>
</head>
<body>
<script type="text/javascript">
javascript codes are written here
</script>
</body>
</html>
Scripts Using an External JavaScript File
If you needs to write same JavaScript code on several pages, you can write it on separate external page with a .js file extension and can link it to the HTML file.You can link external JavaScript file to HTML file as follows.
<html>
<head>
<script src="javascript.js">
</script>
</head>
<body>
</body>
</html>
Read Next: How to Write Conditional Statements in JavaScript?
Related Search Terms
Related Posts:
How To Create Simple Image Slideshow Using JavaScript ?
Image Slideshow with Navigation Buttons Using JavaScript
How to create Changeable Date and Time Using JavaScript?
How to Create JavaScript Image Slideshow with LInks
How to Display Date Format in JavaScript?
How to Validate a HTML Form Using JavaScript?
What are the Different Ways to Redirect Page in JavaScript?
How to Detect Visitor's Browser Using JavaScript?
How to make rounded corners border using CSS
How to Create Custom CSS Template for Printing
How to create a simple form using HTML?