JavaScript is a client side scripting language. Javascript code is downloaded from server and executed on clients browser.
We can include javascript statements in html file or we can create a javascript file and can import it on html files.
In html file we can use script
tag to write javascript statements.
Syntax
1 2 3 | <script type="text/javascript"> javascript statements </script> |
we can write script tag inside head or inside body section.
Example
1 2 3 4 5 6 7 8 9 10 | <html> <head> <title>My First javascript</title> <script type="text/javascript"> document.write("Hello Friends"); </script> </head> <body> </body> </html> |
Result
1 | Hrllo Friends |
We can also put script
tag inside body generally we include as last statement of body
1 2 3 4 5 6 7 8 9 10 11 | <html> <head> <title>My First javascript</title> </head> <body> <h1>Java Script Tutorial</h1> <script type="text/javascript"> document.write("Hello Friends"); </script> </body> </html> |
Result
1 2 | Java Script Tutorial Hello Friends |
Here document refers to body of browser and write() is a function used to write the content on browser body.
We can create a separate javascript file and can include that file in html
We have a javascript file names myJavaScript.js
1 | document.write("Hello Friends"); |
Same folder having file first.html
1 2 3 4 5 6 7 8 9 | <html> <head> <title>My First javascript</title> <script type="text/javascript" src="myJavaScript.js"/> </head> <body> <h1>Java Script Tutorial</h1> </body> </html> |
Here we are including an external javascript file in html file the src attribute must contain path of javascript file.