Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

<html>

<head>

<script type="text/javascript">

function displayDate()

document.getElementById("demo").innerHTML=Date();

</script>

</head>

<body>

<h1>My First Web Page</h1>

<p id="demo">This is a paragraph.</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>

</html>

Validation
<head>

<script type="text/javascript">

function validateForm()
{

var x=document.forms["myForm"]["fname"].value

if (x==null || x=="")

alert("First name must be filled out");

return false;

</script>

</head>

<body>

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">

First name: <input type="text" name="fname">

<input type="submit" value="Submit">

</form>

</body>

</html>

Create an array:
<html>

<body>

<script type="text/javascript">

var i;
var mycars = new Array();

mycars[0] = "Saab";

mycars[1] = "Volvo";

mycars[2] = "BMW";

for (i=0;i<mycars.length;i++)

document.write(mycars[i] + "<br />");

</script>

</body>

</html>

Math max function:

<html>

<body>

<script type="text/javascript">

document.write(Math.max(5,10) + "<br />");

document.write(Math.max(0,150,30,20,38) + "<br />");

document.write(Math.max(-5,10) + "<br />");

document.write(Math.max(-5,-10) + "<br />");

document.write(Math.max(1.5,2.5));

</script>
</body>

</html>

Math min function:

<html>

<body>

<script type="text/javascript">

document.write(Math.min(5,10) + "<br />");

document.write(Math.min(0,150,30,20,38) + "<br />");

document.write(Math.min(-5,10) + "<br />");

document.write(Math.min(-5,-10) + "<br />");

document.write(Math.min(1.5,2.5));

</script>

</body>

</html>

Math round :

<html>

<body>

<script type="text/javascript">
document.write(Math.round(0.60) + "<br />");

document.write(Math.round(0.50) + "<br />");

document.write(Math.round(0.49) + "<br />");

document.write(Math.round(-4.40) + "<br />");

document.write(Math.round(-4.60));

</script>

</body>

</html>

Math random :

<html>

<body>

<script type="text/javascript">

document.write(Math.random() + "<br />");

document.write(Math.floor(Math.random()*11));

</script>
</body>

</html>

Random nos:

<html>

<body>

<script type="text/javascript">

document.write(Math.random() + "<br />");

document.write(Math.floor(Math.random()*11));

</script>

</body>

</html>

You might also like