Welcome to another slide.
We previously coded a lot of HTML while combining the JAVASCRIPT formula. Let's get started using HTML/JAVASCRIPT separate.
JavaScript code is placed in HTML between the <script> and </script> tags.
EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
</body>
</html>
OR
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Body</h2>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
document.getElementById("demo").innerHTML = "My First JavaScript";
A JavaScript function is a piece of JavaScript code that may be executed when it is "called" for.
A function, for example, can be invoked when an event happens, such as when a user hits a button.
An HTML document can include an unlimited number of scripts.
Scripts can be inserted in the body>, head>, or both sections of an HTML page.
A JavaScript function is inserted in the head> section of an HTML page in this example.
When a button is pressed, the function is activated (called):
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>Demo JavaScript in Head</h2>
<p id="demo">Hello There :D</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
JAVASCRIPT
function myFunction() {
document.getElementById("demo").innerHTML = "HOWDYYYYY! :DDDD";
}
A JavaScript function is inserted in the body> section of an HTML page in this example.
When a button is pressed, the function is activated (called):
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>Demo JavaScript in Body</h2>
<p id="demo">Hello There :D</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
JAVASCRIPT
function myFunction() {
document.getElementById("demo").innerHTML = "HOWDYYYYY! :DDDD";
}