How to write inline JavaScript code in HTML file

In this JavaScript tutorial, you will learn how to write inline JavaScript code in HTML file. I will show you this one with an easy example.

But before going to show you how to write inline JavaScript in HTML, I would like to tell you what is inline JavaScript code.

Inline JavaScript Code: If you are adding some JavaScript code in an HTML file without using the  src tag then it is known as inline JavaScript code. That’s all you need to know.

In general, you can add JavaScript code to HTML page in two ways.

  1. Create a js file and add in your HTML file using src tag
  2. Add some JavaScript code directly into HTML file using the <script> but without using src tag.

No 2 is known as inline JavaScript code

Also read,

CSS cursor Property to change cursor type on mouse hover

Bottom Sticky Music Player Source Code in JavaScript and CSS

Write inline JavaScript Code in HTML file

<html>
   <head>
      <title>Title Goes Here</title>
   </head>
   <body>
      <script>
      document.write("here is some text");
      </script>
   </body>
</html>

You can see here I have not used src tag to add JavaScript code. I just used <script> </script> tag

<script>
// Your code goes here...
</script>

You can also use onclick event handler too. This is known as onclick event handler.

Here is an another example of inline JavaScript code,

How To Scroll To A Specific Position of A Webpage in JavaScript (JavaScript Inline Code)

I hope this post is helpful to you. Feel free to comment in the below comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *