How To Javascript - Placement Inward Html File

There is a flexibility given to including JavaScript code anywhere inwards an HTML document. However, the nigh preferred ways to include JavaScript inwards an HTML file are equally follows −
  • Script inwards <head>...</head> section.
  • Script inwards <body>...</body> section.
  • Script inwards <body>...</body> together with <head>...</head> sections.
  • Script inwards an external file together with thus include inwards <head>...</head> section.

In the next section, nosotros volition run into how nosotros tin house JavaScript inwards an HTML file inwards unlike ways.

JavaScript inwards <head>...</head> section

If y'all desire to bring a script run on roughly event, such equally when a user clicks somewhere, thus you
will house that script inwards the caput equally follows −
<htm l>
<head>
<script type="text/javascript">
<!--
function sayHello() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</htm l>
 This code volition create the next results −
There is a flexibility given to including JavaScript code anywhere inwards an HTML document  How to Javascript - Placement inwards Html File

JavaScript inwards <body>...</body> section

If y'all ask a script to run equally the page loads thus that the script generates content inwards the page, then the script goes inwards the <body> percentage of the document. In this case, y'all would non bring any function defined using JavaScript. Take a await at the next code.
<htm l>

<head>
</head>
<body>
<script type="text/javascript">
<!--
docum ent.write("Hello World")
//-->
</script>
<p>This is spider web page torso </p>
</body>
</htm l>
This code volition create the next results − 
There is a flexibility given to including JavaScript code anywhere inwards an HTML document  How to Javascript - Placement inwards Html File


JavaScript inwards <body> together with <head> Sections

You tin position your JavaScript code inwards <head> together with <body> department altogether equally follows −
<htm l>

<head>
<script type="text/javascript">
<!--
role sayHello() {
alert("Hello World")
}
//-->
</script>
</head>
<body>
<script type="text/javascript">
<!--
docum ent.write("Hello World")
//-->
</script>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</htm l>
 This code volition create the next upshot −
There is a flexibility given to including JavaScript code anywhere inwards an HTML document  How to Javascript - Placement inwards Html File

JavaScript inwards External File


As y'all commence to piece of job to a greater extent than extensively amongst JavaScript, y'all volition live on probable to detect that at that topographic point are cases where y'all are reusing identical JavaScript code on multiple pages of a site.

You are non restricted to live on maintaining identical code inwards multiple HTML files. The script tag provides a machinery to permit y'all to shop JavaScript inwards an external file together with thus include it into our HTML files.

Here is an illustration to demonstrate how y'all tin include an external JavaScript file inwards your HTML code using script tag together with its src attribute.
<htm l>
<head>
<script type="text/javascript" src="filenam e.js" ></script>
</head>
<body>
.......
</body>
</htm l>
To usage JavaScript from an external file source, y'all ask to write all your JavaScript source code inwards a unproblematic text file amongst the extension ".js" together with thus include that file equally shown above.

For example, y'all tin continue the next content inwards a filename.js file together with thus y'all tin usage the ayHello role inwards your HTML file later on including the filename.js file.
function sayHello() {
alert("Hello World")
}

Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.