JavaScript is a high-level interpreted programming language which is extensively used in web development.

HTML, CSS and JavaScript are the three main components of a webpageJavaScript allows us to make dynamic interactive web-pagesIn this tutorial we are going to learn how to add JavaScript to a HTML file.

► Play

Open your Html file, we have a simple Html web page with a single button.

When we open this HTML file in the browser, we see the button with the text click me.

The button doesn’t do anything when its clicked. We can use JavaScript to change the behaviour of this button. We will use script tags to insert JavaScript code in the html file. We will place the script tags in theof the HTML page. We can place scripts tags in the,section or both.

We will add the code between the script tags. The type attribute is not required now. Old JavaScript examples often use the type attribute. JavaScript is now the default scripting language in HTML.

We defined an event for the button when the button is clicked it will invoke the function that we defined in between the script tags. The function causes an alert popup with the provided text to appear on the screen whenever the button is placed.

We can put the JavaScript code on another file with the extension ‘.js’ and call the function from the HTML file by using src (source) attribute of the <script> tag. This allows for modularity and reusability of code.

We created a new file jscode.js and copied the function in it.

We then changed the script tag, removed the code inside it and inserted the src attribute.

Now we can make changes in the JavaScript (.js) file without having to edit the HTML file.

Send us a feedback0/500

Do you like this article?
Yes
No