Chapter 4
Chapter 4
• When a user sends a request to the server, then each of that request is
treated as a new request sent by the different user.
• So, to recognize the old user, we need to add the cookie with the response
from the server to browser at the client-side.
• Now, whenever a user sends a request to the server, the cookie is added
with that request automatically. Due to the cookie, the server recognizes
the users.
Creating cookies
• For creating a cookie we need to assign cookie value to
window.document.cookie
• Syntax:
• document.cookie = "key1 = value1;key2 = value2;";
• Here name value pair separated by = sign and terminated by a
delimiter like semicolon(;)
• Example:
• document.cookie = “Username = Kimaya;";
Program for Creating cookies
• <html>
• <head>
• <script>
•
• function CreateCookie
• {
• with(document.form1)
• {
• document.cookie="Username" + username.value + ";" + alert("Cookie is created");
• }
• }
• </script>
• </head>
•
• <body>
• <form name = "form1">
• <input type = "text" name="username" />
• <input type = "button" value = "Create Cookie" onclick = "CreateCookie()"/>
• </form>
• </body>
• </html>
Read a Cookie
function ReadCookie() {
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );
<body>
<form name = "myform" action = "">
<p> click the following button and see the result:</p>
<input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/>
</form>
</body>
</html>
Split()
• Using the split() function the string of cookies is break into key and values.
• The split() method finds the = character in the cookie , and then takes all the
characters to the left of the = and store them into array a[0].
• Next the split() method takes all the characters from the right of the = up to ; but
not including the semicolon and assign those characters to array a[1].
• Thus we can obtain the name value pair in a[0] and a[1] respectively.
Write a cookie
Write a cookie
<head>
<script type = "text/javascript">
function WriteCookie() {
if( document.myform.customer.value == "" ) {
alert("Enter some value!");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie = "name=" + cookievalue;
document.write ("Setting Cookies : " + "name=" + cookievalue );
}
</script>
</head>
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie();"/>
</form>
</body>
</html>
Setting Cookies Expiry Date
• The Universal Coordinated Time (UTC) is the time set by the World
Time Standard.
• Syntax
• Date.toUTCString()
toUTCString() method
<script>
// Here a date has been assigned while creating Date object
var dateobj = new Date('October 15, 2019 05:35:32');
Output:
Tue, 15 Oct 2019 00:05:32 GMT
new Date()
• The Date object is created by using new
keyword, i.e. new Date(). The Date object can
be used date and time in terms of millisecond
precision within 100 million days before or after
1/1/1970.
• If you create a date without any arguments, you
get a date set to the current time (in Local
Time).
• new Date()
• The setTime() method sets the Date object to
the time represented by a number of
milliseconds since January 1, 1970, 00:00:00
UTC
• Syntax
• Date.setTime(millisec)
• The getTime() method returns the number of
milliseconds between midnight of January 1,
1970 and the specified date.
• Syntax
• Date.getTime()
Calculate the number of years since 1970/01/01:
<html>
<body>
<p>Click the button to calculate the number of years since midnight, January 1, 1970.</p>
<p>Click the button to display the UTC date and time as a string.</p>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n = d.toUTCString();
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
• Output:
• Click the button to display the UTC date and time as a string.
function WriteCookie() {
var d = new Date(); //create an date object
d.setTime( d.getTime() + (1000*60*60*24) ); // set the time to the past. 1000 milliseconds=1 second
With (document.myform)
{
document.cookie = "name=" + person.value + “;expires=“ + d.toGMTString();
}
function readCookie()
{
If(document.cookie ==“ “)
document.write(“cookies not found”);
Else
document.write (document.cookie);
}
</script>
</head>
<body>
<form name = "myform" action = "">
Enter your name: <input type = "text" name = “person"/><br>
<input type = "button" value = "Set Cookies" onclick = "WriteCookie()"/>
<input type = "button" value = “Get All Cookies" onclick = “readCookie()"/>
</form>
</body>
</html>
Set cookies with time and get it
<html>
<body>
<script>
// set a cookie
document.cookie=“username=Meenakshi Thalor; expires = Fri,18 dec 2020 12:00:00 GMT ”;
// get a cookie
If(document.cookie)
{
document.write(“Created Cookie is: “+ document.cookie);
cookieString = document.cookie
var list= cookieString.split( “=“ );
document.write(“<br> Split List:” + list);
If( list[0] == “username” )
{
var data = list[1].split( “,” );
document.write(“<br>Data:” +data);
}
}
</script>
</body>
</html>
Example to set and get a cookie.
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
display the cookie's name-value pair separately
<html>
<head>
</head>
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin";
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
</html>
In this example, we provide choices of color and pass the selected color value to the
cookie. Now, cookie stores the last choice of a user in a browser. So, on reloading the
web page, the user's last choice will be shown on the screen.
<body>
<select id="color" onchange="display()">
<option value="Select Color">Select Color</option>
<option value="yellow">Yellow</option>
<option value="green">Green</option>
<option value="red">Red</option>
</select>
<script type="text/javascript">
function display()
{
var value = document.getElementById("color").value;
if (value != "Select Color")
{
document.bgColor = value;
document.cookie = "color=" + value;
}
}
window.onload = function ()
{
if (document.cookie.length != 0)
{
var array = document.cookie.split("=");
document.getElementById("color").value = array[1];
document.bgColor = array[1];
}
}
</script>
</body>
The cookie expires attribute provides one of the ways to create a persistent cookie. Here,
a date and time are declared that represents the active period of a cookie. Once the
declared time is passed, a cookie is deleted automatically.
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin; expires=Sun, 20 Nov 2020 12:00:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
Cookie path attribute
• If a cookie is created for a webpage, by default, it is valid only for the
current directory and sub-directory. JavaScript provides a path attribute to
expand the scope of cookie up to all the pages of a website.
Here, if we create a cookie for webpage2.html, it is valid only for itself and its sub-directory
(i.e., webpage3.html). It is not valid for webpage1.html file.
In this example, we use path attribute to enhance the visibility of cookies up to all the pages. Here, you all just need to do is to
maintain the above directory structure and put the below program in all three web pages. Now, the cookie is valid for each web page.
<body>
<input type="button" value="setCookie" onclick="setCookie()">
<input type="button" value="getCookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username=Duke Martin; max-age=" + (60 * 60 * 24 * 365) + ";path=/;"
}
function getCookie()
{
if(document.cookie.length!=0)
{
var array=document.cookie.split("=");
alert("Name="+array[0]+" "+"Value="+array[1]);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
example to store each name-value pair in a different cookie.
<body>
Name: <input type="text" id="name"><br>
Email: <input type="email" id="email"><br>
Course: <input type="text" id="course"><br>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie = "name=" + document.getElementById("name").value;
document.cookie = "email=" + document.getElementById("email").value;
document.cookie = "course=" + document.getElementById("course").value;
}
function getCookie()
{
if (document.cookie.length != 0)
{
alert("Name="+document.getElementById("name").value+" Email="+document.getElementById("email").value+"
Course="+document.getElementById("course").value);
}
else
{
alert("Cookie not available");
}
}
</script>
</body>
Output
Output
• However, if you click, Get Cookie without filling the form, the below dialog
box appears.
Deleting a Cookie
<body>
<form name = "form1“>
Enter name: <input type = "text" name = “username"/>
<input type = "button" value = “Delete Cookie" onclick = “DeleteCookie()"/>
</form>
</body>
</html>
3 built in functions for setting expiry date of cookie
<html>
<head>
<script type = "text/javascript">
<!--
function WriteCookie() {
var now = new Date();
now.setMonth( now.getMonth() - 1 );
cookievalue = escape(document.my
form.customer.value) + ";"
<body>
<form name = "myform" action = "">
Enter name: <input type = "text" name = "customer"/>
<input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/>
</form>
</body>
</html>
Set new expiry date of cookies
<html>
<head>
<script>
Function SetExpDtCookie()
{
expiryDate=new Date
expiryDate.setMonth(expiryDate.getMonth()+1)
With(document.form1)
{
Document.cookie=“UserName” +username.value+”;”
Document.cookie=“expires=“ +expiryDate.toUTCString()+”;”
Alert(“New expiry Date is set for the Cookie”);
}
}
</script>
</head>
<body>
<form name=“form1”>
Enter name:<input type=“text” name=“username”/>
<input type=“button” value=“Set New Expiry Date” onclick=“SetExpDtCookie()”/>
</form>
</body>
</html>
Browser
• A browser is an application program that provides a way look at and
interact with all the information on the World Wide Web(WWW).
• Once the new browser window is set up, it is also possible to change the
contents within that window dynamically.
Methods of window object
• alert() in javascript
• <script type="text/javascript">
• function msg(){
• alert("Hello Alert Box");
• }
• </script>
• <input type="button" value="click" onclick="msg()"/>
• confirm() in javascript
• It displays the confirm dialog box. It has message with ok and cancel buttons.
• <script type="text/javascript">
• function msg(){
• var v= confirm("Are u sure?");
• if(v==true){
• alert("ok");
• }
• else{
• alert("cancel");
• }
•
• }
• </script>
•
• <input type="button" value="delete record" onclick="msg()"/>
• prompt() in javascript
• It displays prompt dialog box for input. It has message and textfield.
• <script type="text/javascript">
• function msg(){
• var v= prompt("Who are you?");
• alert("I am "+v);
•
• }
• </script>
•
• <input type="button" value="click" onclick="msg()"/>
Opening window
• It is possible to open a new window from a
JavaScript by simply clicking a button.
• For that purpose the window object is used.
• This window object has various useful
properties and methods.
• To open a new window we use open() method
of window object.
Window open() Method
• The open() method opens a new browser window, or a new tab, depending on your
browser settings and the parameter values.
• Syntax
window.open(URL, name, style)
• URL- Optional. Specifies the URL of the web page to open. If no URL is specified, a new
window/tab with about:blank is opened
• Name-Optional. Specifies the target attribute or the name of the window. The
following values are supported:
_blank - URL is loaded into a new window, or tab. This is default
_parent - URL is loaded into the parent frame
_self - URL replaces the current page
_top - URL replaces any framesets that may be loaded
name - The name of the window (Note: the name does not specify the title of
the new window)
• Style- It includes various parameters such as toolbar,scrollbar, height, width of window
and so on.
Various styles of window
Layout of window
Program to open a new window
hello.html
<html>
<body>
<script>
document.write(“Hello everybody!!!!”);
</script>
</body>
</html>
Sample.html
<html>
<body>
<script>
var winob=window.open(“hello.html”, “windowName”,
“top=200,left=100,width=250,height=100,status”);
</script>
</body>
</html>
Program to open a new window
<html>
<head>
<script>
function myFunction() {
var mywin= window.open(“https://www.google.com", “windowName",
"top=200,left=100,width=250,height=100,status");
}
</script>
</head>
<body>
<form name=“form1” method=“post”>
</form>
</body>
</html>
Program to open a new window
<html>
<body>
<p>Click the button to open a new window with some specifications.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open("https://www.w3schools.com", "_blank", "toolbar=yes, scrollbars=yes,
resizable=yes, top=500,left=500,width=400,height=400");
}
</script>
</body>
</html>
Output:
Click the button to open a new window with some specifications .
Close window
Window.close()
This method is used to close the window which are opened by window.open()
method.
Syntax:
window.close()
<html>
<body>
<script>
var myWindow;
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
function closeWin() {
myWindow.close();
}
</script>
</body>
</html>
Close window
<html>
<head>
<title>
window open and close method
</title>
<script>
var Window;
• Syntax:
• Window.blur()
• <html>
• <head>
• <title>
• window Blur and Focus method
• </title>
• <style>
• body {
• text-align: center;
• }
•
• .gfg {
• font-size: 40px;
• font-weight: bold;
• color: green;
• }
• </style>
• </head>
• <body>
• <div class="gfg">GeeksforGeeks</div>
• <h2>Blur and Focus</h2>
• <script>
• var Window;
•
• // Function that open the new Window
• function windowOpen() {
• Window = window.open(
• "https://www.geeksforgeeks.org/",
• "_blank", "width=400, height=450");
• }
•
Output
Window position
• A new window always displayed on the screen
at the location which is specified by user and
location is specified by setting the left and top
properties of the new window .
• User can make change in the features or
dimensions of window as per the requirement
but a new window is always created on top of
the current window after the button is clicked.
Retrieving window properties
<html>
<head>
<script>
1. hash
2. host
3. hostname
4. href
5. origin
6. pathname
7. port
8. protocol
9. search
• Methods of location object:
1. assign( )
2. reload( )
3. replace( )
• window.location.href- returns the href (URL) of the current
page
• window.location.host-name returns the domain name of the
web host
• window.location.pathname -returns the path and filename of
the current page
• window.location.protocol -returns the web protocol used
(http: or https:)
• window.location.assign() -loads a new document
• The window.location.port- property returns the number of
the internet host port (of the current page).
To display the pathname of the web page
<html>
<body>
<p id=“ID”> </p>
<script>
document.getElementById(“ID”).innerHTML= “This web page is at path:” + window.location.pathname;
</script>
</body>
</html>
To display the protocol of the web page
<html>
<body>
<p id=“ID”> </p>
<script>
document.getElementById(“ID”).innerHTML= “This web page is using the protocol:” +
window.location.protocol;
</script>
</body>
</html>
Window Location Assign
• <html>
• <head>
• <script>
• function newDoc() {
• window.location.assign("https://www.w3schools.com")
• }
• </script>
• </head>
• <body>
• </body>
• </html>
Window History object
• The window.history object is used for displaying
browser history.
• There are two important methods of window.browser.
• 1) window.history.back() : This method loads the
previous URL in the history list. It is like clicking Back
button on browser window.
• 2) window.history.forward() : This method loads the
next URL in the history list. It is like clicking Forward
button on browser window.
Display browser history
<html>
<head>
<script>
Function MoveBack()
{
window.history.back();
}
Function MoveForward()
{
window.history.forward();
}
</script>
</head>
<body>
<form name=“form1”>
<input type=“button” value=“Back” onclick=“MoveBack()” />
<input type=“button” value=“Forward” onclick=“MoveForward()” />
</body>
</html>
Changing the contents of Window
• We can change the content of opened new
window as when require.
• We can change the content of the window by
calling the open() method using the same
window name each time.
• By writing some text to the newly created
window we can change the contents of a
window.
JavaScript to write some text using write method
<html>
<head>
<script>
function myFunction() {
var myWindow = window.open("", "myWin“, “width=200”,height=200”);
myWindow.document.write("<p>This line is written in current window</p>");
}
</script>
</head>
<body>
<form>
<input type=“button” value=“Open Window” onclick="myFunction()“/>
</form>
</body>
</html>
Output
Changing the contents of Window
<html>
<body>
<script>
function OpenNewWindow(Ad) {
var winobj = window.open(Ad, "myWin“, “top=200, left=200, width=250”,height=250”);
}
</script>
</body>
</html>
Output
Scrolling a web page
• We can scroll horizontally or vertically using scrollTo()
function.
<html>
<head>
<style>
body {
width: 5000px;
}
</style>
</head>
<body>
<p>Click the button to scroll the document window to 500 pixels horizontally.</p>
<script>
function scrollWin() {
window.scrollTo(500, 0);
}
</script>
</body>
</html>
Output
• Click the button to scroll the document window to 500 pixels
horizontally.
Scrolling a web page
<html>
<head>
<script>
function ScrollFunction()
{
window.scrollTo(300,0)
}
</script>
</head>
<body>
<form>
<input type=“button” value=“Scroll ” onclick=“ScrollFunction()“/>
</form>
</body>
</html>
Multiple windows at a glance
<html>
<head>
<script>
function OpenWindowFunction()
{
for (i=0;i<5; i++)
{
var myWindow = window.open("", "myWin“+i, “width=100”,height=100”);
}
}
</script>
</head>
<body>
<form>
<input type=“button” value=“Open Window” onclick=“OpenWindowFunction() “ />
</form>
</body>
</html>
Output
Creating a web page in new window
• 1000 ms = 1 second.
• Syntax
setInterval(function, milliseconds, param1, param2, ...)
• Parameter Values
Parameter Description
function Required. The function that will be executed
milliseconds Required. The intervals (in milliseconds) on how often to execute the
code. If the value is less than 10, the value 10 is used
param1, Optional. Additional parameters to pass to the function
param2, ...
Alert "Hello" every 3 seconds
<html>
<body>
<script>
function myFunction() {
setInterval(function() { alert("Hello"); }, 3000);
}
</script>
</body>
</html>
Output:
<html>
<body>
<script>
var myVar;
function myFunction()
{
myVar = setInterval (alertFunc, 3000);
}
function alertFunc( )
{
alert("Hello!");
}
</script>
</body>
</html>
Display welcome message repeatedly after some specific time interval
<html>
<body>
<p id=“ID”></p>
<script>
var t= setInterval(MyFunction,2000);
function MyFunction()
{
document.getElementById(“ID”).innerHTML + =“<p> Welcome User!!! </p>” ;
}
</script>
</body>
</html>
Display the current time
(the setInterval() method will execute the function once every 1 second, just like a digital watch):
<html>
<body>
<script>
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
</script>
</body>
</html>
Output:
A script on this page starts this clock:
7:06:25 PM
Using clearInterval() to stop time in the previous example:
<html>
<body>
<script>
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
function myStopFunction() {
clearInterval(myVar);
}
</script>
</body>
</html>
Output:
A script on this page starts this clock:
7:09:07 PM
Writing number after a delay using setInterval() method
<html>
<head>
<script>
var num=0;
var timerID=null;
function magic() {
If(timerID===null){ // to avoid multiple registration
timerID= setInterval(“display()” , 500);
}
}
function display() {
If(num>15){
clearInterval(timerID);
return;
}
document.getElementById(“ output “).innerHTML +=num;
num++;
}
</script>
</head>
<body>
<input type=“button” value=“Click Me” onclick=“magic()” />
<textarea id=“ output”></textarea>
</body>
</html>
Toggle between two background colors once every 300 milliseconds:
<html>
<body>
<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds, which will toggle between two background colors.</p>
<script>
var myVar = setInterval(setColor, 300);
function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myVar);
}
</script>
</body>
</html>
• https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval2
Pass parameters to the alertFunc function
<html>
<body>
<p>In this example, we also output the parameters that were passed to the alertFunc() function (does not work in IE9 and earlier).</p>
<p id="demo"></p>
<script>
var myVar;
function myStartFunction() {
myVar = setInterval(alertFunc, 2000, "First parameter", "Second parameter");
}
function myStopFunction() {
clearInterval(myVar);
}
</script>
</body>
</html>
Output
Click the Start button to output "Hello" once every 2 seconds.
In this example, we also output the parameters that were passed to the
alertFunc() function
Window setTimeout() Method
• Syntax
• setTimeout(function, milliseconds, param1, param2, ...)
• Parameter Values
Parameter Description
function Required. The function that will be executed
milliseconds Optional. The number of milliseconds to wait before executing the code.
If omitted, the value 0 is used
param1, Optional. Additional parameters to pass to the function (Not supported
param2, ... in IE9 and earlier)
Program for Alert popup will be displayed 2 seconds
after clicking the button
<html >
<head>
<title>JavaScript Execute a Function after Some Time</title>
</head>
<body>
<script>
function myFunction() {
alert('Hello World!');
}
</script>
<html>
<body>
<script>
function myFunction() {
setTimeout(function(){ alert("Hello"); }, 3000);
}
</script>
</body>
</html>
• Output:
• Click the button to wait 3 seconds, then alert "Hello".
You can also refer to "named" function; Display an alert box after
3 seconds
<html>
<body>
<script>
var myVar;
function myFunction() {
myVar = setTimeout(alertFunc, 3000);
}
function alertFunc() {
alert("Hello!");
}
</script>
</body>
</html>
• Output:
• Click the button to wait 3 seconds, then alert "Hello".
Display a timed text:
<html>
<body>
<p>Click on the button below. The input field will tell you when two, four, and six seconds have passed.</p>
<script>
function timedText() {
var x = document.getElementById("txt");
setTimeout(function(){ x.value="2 seconds" }, 2000);
setTimeout(function(){ x.value="4 seconds" }, 4000);
setTimeout(function(){ x.value="6 seconds" }, 6000);
}
</script>
</body>
</html>
• Output:
Click on the button below. The input field will tell you when two, four, and six seconds have passed.
Open a new window and close the window after three seconds
<html>
<body>
<p>Click the button to open a new window and close the window after three seconds (3000
milliseconds)</p>
<script>
function openWin() {
var myWindow = window.open("", "myWindow", "width=200, height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
setTimeout(function(){ myWindow.close() }, 3000);
}
</script>
</body>
</html>
• Output:
Click the button to open a new window and close the window after three seconds (3000 milliseconds)
A clock created with timing events:
<html>
<body onload="startTime()">
<div id="txt"></div>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
// add a zero in front of numbers<10
m = checkTime(m);
s = checkTime(s);
document.getElementById("txt").innerHTML = h + ":" + m + ":" + s;
var t = setTimeout(function(){ startTime() }, 500);
}
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
</script>
</body>
</html>
• Output:
23:00:54
Rotating images using setTimeout() method after 1 sec.
<html>
<head>
<script>
var I,imgs,pic;
function init()
{
pic=document.getElementById(“pic” );
imgs=[“query.png”, “warn.png”, “stop.png” , “info.png” ];
i=0;
rotate();
}
function rotate()
{
pic.src=imgs[i];
(i==(imgs.length-1)) ? i=0; i++;
setTimeout(rotate,1000);
}
document.addEventListner(“DOMContentLoaded”, init, false);
</script>
</head>
<body>
<img id=“pic” width=“120” height=“120” src=“Javascript.jpg”>
</body>
</html>
Moving a car using setTimeout() and clearTimeout() methods
<html>
<head>
<script>
var obj=null;
var animate;
function init()
{
obj=document.getElementById(‘car’ );
obj.style.position=“relative”;
obj.style.left=‘0px’;
}
function start()
{
obj.style.left=parseInt(obj.style.left)+10+‘px’;
Animate=
}
document.addEventListner(“DOMContentLoaded”, init, false);
</script>
</head>
<body>
<img id=“pic” width=“120” height=“120” src=“Javascript.jpg”>
</body>
</html>