1. The <script> Tag
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<h3>javascript in body</h3>
<p id="demo"> </p>
</body>
<script>
document.getElementById("demo").innerHTML="my first JavaScript";
</script>
</html>
2. Window alert() Method
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<p>Click the button to display an alery box.</p>
<button onClick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("hello i am an alert box");
}
</script>
</body>
</html
3. Javascript - alert
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<head>
<script>
function validateForm(){
var x = document.forms["myForm"]"fname"].value;
if (x == "") {
alert("name must be filled out");
return false;
}
}
</script>
</head>
<body>
<h1>Is Alert</h1>
<form name="myForm" action="/action_page_post.php" onsubmit="return validate()" method="post">
name: <input type="text" name="frame">
<input type="submit" value="submit">
</form>
</body>
</html>
4. JavaScript Can Validate Numeric Input
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<head>
<style>
#myBtn {
display: none;
position: fixed;
button: 20px;
right: 30px;
z-index: 99;
outline: none;
background-color: red;
color: white;
cursor: pointer;
padding: 15px;
border-radius: 10px;
}
#myBtn:hover;{
background-color: #555;
}
</style>
</head>
<body>
<button onclick="topFunction()" id="myBtn" title="go to top">top</button>
<div style="background-color:black;color:white;padding:30px">Scroll Down</div>
<div style="background-color:lightgrey;padding:30px 30px 2500px">this example demonstrates how to create a "scroll to top" button that becomes visible when the user starts to scroll to page </div>
<script>
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop> 20||document.documentElement.scrollTop> 20){ document.getElementById("myBtn").style.display = "block";
}else{ document.getElementById("myBtn").style.display = "none";
}
}
function topFunction(){
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}

Komentar
Posting Komentar