There are mainly two ways from which we can add javascript inside web page as follows:
A) Inside <head> tag.
- First Way: If you will have page specific as well as script with less number of lines, it is good to use this way.
<html> <head> <script> function DemoFunction() { alert("My First JavaScript Function"); } </script> </head> <body> <button type="button" onclick="DemoFunction()">Click Me</button> </body> </html>
- Second Way: If you want to reuse a script on multiple pages, you should follow this way.
<html> <body> <script src="DemoScript.js"></script> </body> </html>
B) Inside <body> tag.
<!DOCTYPE html> <html> <head> </head> <body> <h1 id="h1">My Web Page</h1> <button type="button" onclick="demoFunction()">Try it</button> <script> function demoFunction() { document.getElementById("h1").innerHTML="This is been implemented from inside <body> tag"; } </script> </body> </html>