There square measure 3 general areas that JavaScript are often placed to be used during a webpage.
- Inside the pinnacle tag
- Within the body tag (like our example within the previous lesson)
- In associate external file (we'll cite this next lesson)
The location alternative of head or body is extremely straightforward. If you wish to possess a script run on some event, like once a user clicks somewhere, then you'll place that script within the head. If you wish the script to run once the page hundreds, like our "Hello World!" example within the previous lesson, then you'll wish to position the script among the body tag.
External JavaScript files and their uses are going to be mentioned within the next lesson.
Example head script:
Since we've already seen the type of script that goes within the body, however concerning we have a tendency to write a script that takes place once some event occurs? Let's have associate alert show up once a user click on a button.
HTML and JavaScript Code:
<html>
<head>
<script type="text/JavaScript">
function popup() {
alert("Hello My JavaScript Tutorials")
}
</script>
</head>
<body>
<input type="button" onclick="popup()" value="popup">
</body>
</html>
We created a perform referred to as popup and placed it within the head of the hypertext markup language document. currently whenever somebody clicks on the button (this is associate event), associate alert can pop with "Hello World!". we'll get in additional depth on functions and events during a later lesson.
No comments:
Post a Comment