1.
<script>
2. document.write("Hello JavaScript by JavaScript");
3. </script>
Places to put JavaScript code
1. Between the body tag of html
2. Between the head tag of html
3. In .js file (external javaScript)
4. <html>
5. <body>
6. <script type="text/javascript">
7. alert("Hello Javatpoint");
8. </script>
9. </body>
10. </html> <html>
11. <head>
12. <script type="text/javascript">
13. function msg(){
14. alert("Hello Javatpoint");
15. }
16. </script>
17. </head>
18. <body>
19. <p>Welcome to Javascript</p>
20. <form>
21. <input type="button" value="click" onclick="msg()"/>
22. </form>
23. </body>
24. </html>
The script tag specifies that we are using JavaScript.
The text/javascript is the content type that provides information to the
browser about the data.
The document.write() function is used to display dynamic content
through JavaScript. We will learn about document object in detail later.
Javascript - document.getElementById()
method
The document.getElementById() method returns the element of specified
id.
1. <script type="text/javascript">
2. function getcube(){
3. var number=document.getElementById("number").value;
4. alert(number*number*number);
5. }
6. </script>
7. <form>
8. Enter No:<input type="text" id="number" name="number"/><br/>
9. <input type="button" value="cube" onclick="getcube()"/>
10. </form>