Where to Insert JavaScript
In this article I am going to explain about where to use JavaScript in our document.
JavaScript Where to Insert
For inserting JavaScript in Html document we use <script> tag in document. We can insert JavaScript in <Head> and <Body> section of Html document. We can also also use External JavaScript In our document.
- For using External document we use <script> tag with "source" attribute of JavaScript.
- External JavaScript save with .Js external. (For example Myfile.Js.
- For using external JavaScript source path must be require.
Using JavaScript in <Head> section.
<html>
<head>
<script type="text/jscript">
function Myfunction() {
document.write("JavaScript in HEad section");
</script>
</head>
<body>
<button onclick="Myfunction()">
click me</button>
</body>
</html>
|
Using JavaScript in <body> section.
<html>
<body>
<button type="button" onclick="Myfun()">
Click me</button>
<script type="text/javascript">
function Myfun() {
document.write("JavaScript in body section");
}
</script>
</body>
</html>
|
Using External JavaScript //In this example MyDoc.Js is external JavaScropt file.
<html>
<head>
<script type="text/javascript" src="MyDoc.Js"></script>
</head>
<body>
</body>
</html>
|
Output of <body> section
Further Readings
You may also want to read these related articles: here