May 7, 2013

Using the max function

Coding

<html>
<body>
<script language="JavaScript">
document.write(Math.max(2,4))
</script>
</body>
</html>

Displaying random number between 1-10

Coding

<html>
<body>
<script language="JavaScript">
no=Math.random()*10
document.write(Math.round(no))
</script>
</body>
</html>

Using the random function

Coding

<html>
<body>
<script language="JavaScript">
no=Math.random()*10
document.write(Math.round(no))
</script>
</body>
</html>

Using the round function

Coding

<html>
<body>
<script language="JavaScript">
document.write(Math.round(7.25))
</script>
</body>
</html>

Display timer

Coding

<html>
<head>
<script Language="JavaScript">
var timer = null
function stop()
{
clearTimeout(timer)
}
function start()
{
var time = new Date()
var hours = time.getHours()
var minutes = time.getMinutes()
var seconds = time.getSeconds()
var clock = hours
clock += ((minutes < 10) ? ":0" : ":") + minutes
clock += ((seconds < 10) ? ":0" : ":") + seconds
document.forms[0].display.value = clock;
timer = setTimeout("start()",1000);
}
</script>
</head>
<body onload="start()" onunload="stop()">
<form>
<input type="text" name="display">
</form>
</body>
</html>

Display Full date of the date

Coding

<html>
<body>
<script language="JavaScript">
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + ". ")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getFullYear())
</script>
</body>
</html>

Finding weekday of a particular date

Coding

<html>
<body>
<script language="JavaScript">
var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
document.write("Today is " + weekday[d.getDay()])
</script>
</body>
</html>

Setting Date in UTC Format

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
document.write(d.getUTCHours())
document.write(".")
document.write(d.getUTCMinutes())
document.write(".")
document.write(d.getUTCSeconds())
</script>
</body>
</html>

Setting Date in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
d.setFullYear("1990")
document.write(d)
</script>
</body>
</html>

Time in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
document.write(d.getHours())
document.write(".")
document.write(d.getMinutes())
document.write(".")
document.write(d.getSeconds())
</script>
</body>
</html>

May 6, 2013

Using Dates in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
document.write(d.getDate())
document.write(".")
document.write(d.getMonth() + 1)
document.write(".")
document.write(d.getFullYear())
</script>
</body>
</html>

Another example of using Arrays in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var famname = new Array("Jan Egil","Tove","Hege","Stale","Kai Jim","Borge")
for (i=0; i<famname.length; i++)
{
document.write(famname[i] + "<br>")
}
</script>
</body>
</html>

Using Arrays in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var famname = new Array(6)
famname[0] = "Jan Egil"
famname[1] = "Tove"
famname[2] = "Hege"
famname[3] = "Stale"
famname[4] = "Kai Jim"
famname[5] = "Borge"
for (i=0; i<6; i++)
{
document.write(famname[i] + "

Using substr function with string

Coding

<html>
<body>
<script language="JavaScript">
var str="W3Schools are great"
document.write(str.substr(3,6))
</script>
</body>
</html>

Converting the case of Strings

Coding

<html>
<body>
<script language="JavaScript">
var str=("Hello JavaScripters!")
document.write(str.toLowerCase())
document.write("<br>")
document.write(str.toUpperCase())
</script>
</body>
</html>

Using String Match

Coding

<html>
<body>
<script language="JavaScript">
var str = "W3Schools are great"
document.write(str.match("great"))
</script>
</body>
</html>

Using Index of Method in String

Coding

<html>
<body>
<script language="JavaScript">
var str="W3Schools"
var character=str.indexOf("School")
document.write(character + "<br>")
if (character>=0) {
document.write("The string contains the specified character")
}
</script>
</body>
</html>

Finding String Length

Coding

<html>
<body>
<script language="JavaScript">
var str="how many characters"
document.write(str.length)
</script>
</body>
</html>

Using DoWhile Loop

Coding

<html>
<body>
<script language="JavaScript">
i = 0
do
{
document.write("The number is " + i)
document.write("<br>")
i++
}
while (i <= 5)
</script>
</body>
</html>

Explanation:

i equal to 0.

The loop will run

i will increase by 1 each time the loop runs.

While i is less than , or equal to, 5, the loop will continue to run.

Using While Loop

Coding

<html>
<body>
<script language="JavaScript">
i = 0
while (i <= 5)
{
document.write("The number is " + i)
document.write("<br>")
i++
}
</script>
</body>
</html>

Explanation

i equal to 0.

While i is less than , or equal to, 5, the loop will continue to run.

i will increase by 1 each time the loop runs.

Using For Loop

Coding

<html>
<body>
<script language="JavaScript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">This is header " + i)
document.write("</h" + i + ">")
}
</script>
</body>
</html>

Switch Statement

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
theDay=d.getDay()
switch (theDay)
{
case 5:
document.write("Finally Friday")
break
case 6:
document.write("Super Saturday")
break
case 0:
document.write("Sleepy Sunday")
break
default:
document.write("I'm really looking forward to this weekend!")
}
</script>

<p>This example demonstrates the switch statement.</p>
<p>You will receive a different greeting based on what day it is.</p>
<p>Note that Sunday=0, Monday=1, Tuesday=2, etc.</p>
</body>
</html>

If Else statement in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
else
{
document.write("<b>Good day</b>")
}
</script>
<p>
This example demonstrates the If...Else statement.<p>If the time on your browser is
less then 10, you will get a "Good morning" greeting. Otherwise you
will get a "Good day" greeting.
</body>
</html>

Using If Statement

Coding

<html>
<body>
<script language="JavaScript">
var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("<b>Good morning</b>")
}
</script>
<p>
This example demonstrates the If statement.<p>If the time on your browser is
less then 10, you will get a "Good morning" greeting.
</body>
</html>

Function with arguments that return a value

Coding

<html>
<head>
<script language="JavaScript">
function total(numberA,numberB)
{
return numberA + numberB
}
</script>
</head>
<body>
<script language="JavaScript">
document.write(total(2,3))
</script>
<p>The script in the body section calls a function with two arguments, 2 and 3.</p>
<p>The function returns the sum of these two arguments.</p>
</body>
</html>

Function that returns a value

Coding

<html>
<head>
<script language="JavaScript">
function myFunction()
{
return ("Hello, have a nice day!")
}
</script>
</head>
<body>
<script language="JavaScript">
document.write(myFunction())
</script>
<p>The script in the body section calls a function.</p>
<p>The function returns a text.</p>
</body>
</html>

Function with two Arguments

Coding

<html>
<head>
<script language="JavaScript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button" onclick="myfunction('Good Morning!')" value="In the Morning">
<input type="button" onclick="myfunction('Good Evening!')" value="In the Evening">
</form>
<p>
When you click on one of the buttons, a function will be called. The function will alert
the argument that is passed to it.
</p>
</body>
</html>

Function with Arguments

Coding

<html>
<head>
<script language="JavaScript">
function myfunction(txt)
{
alert(txt)
}
</script>
</head>
<body>
<form>
<input type="button" onclick="myfunction('Hello')" value="Call function">
</form>
<p>By pressing the button, a function with an argument will be called. The function will alert this argument.</p>
</body>
</html>

Creating Functions in JavaScript

Coding

&lt;html&gt; &lt;head&gt; &lt;script language=&quot;JavaScript&quot;&gt; function myfunction() { alert(&quot;HELLO&quot;) } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; &lt;input type=&quot;button&quot; onclick=&quot;myfunction()&quot; value=&quot;Call function&quot;&gt; &lt;/form&gt; &lt;p&gt;By pressing the button, a function will be called. The function will alert a message.&lt;/p&gt; &lt;/body&gt; &lt;/html&gt;

Using Variables in JavaScript

Coding

<html>
<body>
<script language="JavaScript">
var name = "Hege"
document.write(name)
document.write("<h1>"+name+"</h1>")
</script>
<p>This example declares a variable, assigns a value to it, and then displays the variable.</p>
<p>Then the variable is displayed one more time, only this time as a heading.</p>
</body>
</html>

Popular Posts

Views