You are here: JAVAScript/introduction
JavaScript is the programming language for developing web application. Example: <script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
JavaScript in <head>: In this example, a JavaScript function is in the <head> section of an HTML page. <!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("nicedemo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="nicedemo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
In this example, a JavaScript function is in the <body> section of an HTML page. <!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>My Web Page</h1>
<p id="nicedemo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("nicedemo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
External JavaScript:
To use an external script, put the name of the script file with path in the src (source) attribute of the <script> tag: <!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>
You can place an external script reference in <head> or <body> as you require.
|
BlogActive User (1) |