JavaScript is the world's most popular programming language.
It is the language for HTML and the web, for servers,
PCs, laptops, tablets, smart phones, and more.
JavaScript is a Scripting Language
A scripting language is a lightweight programming language.JavaScript is programming code that can be inserted into HTML pages.
JavaScript inserted into HTML pages, can be executed by all modern web browsers.
JavaScript is easy to learn.
The <script> Tag
To insert a JavaScript into an HTML page, use the <script> tag.The <script> and </script> tells where the JavaScript starts and ends.
The lines between the <script> and </script> contain the JavaScript:
<script>
alert("My First JavaScript");
</script>
alert("My First JavaScript");
</script>
JavaScript in <body>
In this example, JavaScript writes into the HTML <body> while the page loads:Example
<!DOCTYPE html>
<html>
<body>
.
. <script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script>
.
.
</body>
</html>
<html>
<body>
.
. <script>
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph</p>");
</script>
.
.
</body>
</html>
JavaScript in <head> or <body>
You can place an unlimited number of scripts in an HTML document.Scripts can be in the <body> or in the <head> section of HTML, and/or in both.
It is a common practice to put functions in the <head> section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.
A JavaScript Function in <head>
In this example, a JavaScript function is placed in the <head> section of an HTML page.The function is called when a button is clicked:
Example
<!DOCTYPE html>
<html><head> <script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
<html><head> <script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First JavaScript Function";
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
No comments: