Javascript is a verstatile programming language that plays a crucial role in creating interactive and dynamic web experiences. It's one of the core technologies of the web, alongside HTML and CSS.
There are two primary ways to call javascript functions within HTML:
Before that, please make a file with .js extension in your root folder. For example, we use 'script.js' name file.
-
Inline Event Handlers
You can assign Javascript functions to HTML elements using event attributes like onClick, onMouseOver, onLoad, etc.
You can define myFunction() within a ❮script❯ tag in your HTML file. When the button is clicked, the function will be executed.
placement of script tags:
- In the ❮head❯ tag: Use this if the script needs to be loaded before the page content.
- In the end of ❮body❯: Use this if the script relies on DOM elements that are loaded after the page content.
-
External Javascript File
Use the ❮script❯ tag with src attribute to link an external javascript file:
In your script.js file: