Javascript Example
Javascript Example
JavaScript Example
Within body tag
Within head tag
Javascript example is easy to code. JavaScript provides 3 places to put the
JavaScript code: within body tag, within head tag and external JavaScript file.
<script type="text/javascript">
document.write("JavaScript is a simple language for javatpoint learners");
</script>
Test it Now
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.
ADVERTISEMENT
In the above example, we have displayed the dynamic content using JavaScript. Let’s
see the simple example of JavaScript that displays alert dialog box.
<script type="text/javascript">
alert("Hello Javatpoint");
</script>
Test it Now
Let’s see the same example of displaying alert dialog box of JavaScript that is
contained inside the head tag.
To call function, you need to work on event. Here we are using onclick event to
call msg() function.
<html>
<head>
<script type="text/javascript">
function msg(){
alert("Hello Javatpoint");
}
</script>
</head>
<body>
<p>Welcome to JavaScript</p>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>