06-Dom & Bom
06-Dom & Bom
06-Dom & Bom
1
4/27/2015
2
4/27/2015
3
4/27/2015
4
4/27/2015
5
4/27/2015
Example :
<body>
<p id="p1">Hello World!</p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
6
4/27/2015
Example :
<body>
<img id="myImage" src="smiley.gif">
<script>
document.getElementById("myImage").src =
"landscape.jpg";
</script>
</body>
7
4/27/2015
Examples
<body>
<img src="http://oi44.tinypic.com/30u3e52.jpg" id="pic1" />
<br>
<input type="button" value="Ship" onclick="sh()" />
<input type="button" value="Plane" onclick="pl()" />
<input type="button" value="Hide" onclick="h()" />
<input type="button" value="Show" onclick="s()" />
<script type="text/javascript">
var x=document.getElementById("pic1");
function sh() { x.src="http://oi44.tinypic.com/9te69k.jpg"; return; }
function pl() { x.src="http://oi44.tinypic.com/30u3e52.jpg"; return; }
function h() { x.style.display="none"; return; }
function s() { x.style.display="inline"; return; }
</script>
</body>
8
4/27/2015
Reacting to Events
9
4/27/2015
DOM Nodes
According to the W3C HTML DOM standard,
everything in an HTML document is a node, that is :
The entire document is a document node.
Every HTML element is an element node.
The text inside HTML elements are text nodes.
Every HTML attribute is an attribute node.
All comments are comment nodes.
With the HTML DOM, all nodes in the node tree can
be accessed by JavaScript.
New nodes can be created, and all nodes can be
modified or deleted.
Node Relationships
10
4/27/2015
11
4/27/2015
12
4/27/2015
13
4/27/2015
14
4/27/2015
Examples
<body>
<h1>Name Validator</h1>
<label for="n1">Please enter your name : </label>
<input type="text" id="n1" length="40" /><br>
<input type="button" value="Validate" onclick="pl()" />
<script type="text/javascript">
var x=document.getElementById("n1");
function pl(){
var y=x.value;
y=y.replace(/[0-9]/gi, "");
y=y.replace(/\u0027/gi, "`");
x.value=y;
}
</script>
</body>
15
4/27/2015
Window Size
Three different properties can be used to determine the size of
the browser window (NOT including toolbars and scrollbars).
For Internet Explorer, Chrome, Firefox, Opera, and Safari:
window.innerHeight - the inner height of the browser window
window.innerWidth - the inner width of the browser window
For Internet Explorer 8, 7, 6, 5:
document.documentElement.clientHeight
document.documentElement.clientWidth
or
document.body.clientHeight
document.body.clientWidth
16
4/27/2015
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
Window Methods
window.open() - open a new window.
window.close() - close the current window.
window.alert() - displays a dialog box with a message
and an OK button.
window.confirm() - displays a dialog box with a message
and an OK and a Cancel button.
window.prompt() - displays a dialog box that prompts the
visitor for input.
window.print() - prints the content of the current window.
window.moveTo() - move the current window.
window.resizeTo() - resize the current window.
window.scrollTo() - scrolls the document to the specified
coordinates.
17
4/27/2015
18
4/27/2015
19
4/27/2015
20
4/27/2015
21
4/27/2015
22
4/27/2015
23
4/27/2015
24
4/27/2015
Timing Events
With JavaScript, it is possible to execute some code
at specified time-intervals. This is called timing
events.
It's very easy to time events in JavaScript. The two
key methods that are used are:
setInterval() - executes a function, over and over
again, at specified time intervals.
setTimeout() - executes a function, once, after waiting
a specified number of milliseconds.
The setInterval() and setTimeout() are
both methods of the HTML BOM Window object.
25
4/27/2015
The clearInterval()
Method
The clearInterval() method is used to stop
further executions of the function specified in the
setInterval() method.
The window.clearInterval() method can be
written without the window prefix.
To be able to use the clearInterval() method,
you must use a global variable when creating the
interval method.
Then you will be able to stop the execution by
calling the clearInterval() method.
Syntax :
window.clearInterval(intervalVariable)
26
4/27/2015
The clearTimeout()
Method
The clearTimeout() method is used to stop the
execution of the function specified in the
setTimeout() method.
The window.clearTimeout() method can be
written without the window prefix.
To be able to use the clearTimeout() method,
you must use a global variable when creating the
timeout method.
Then, if the function has not already been executed,
you will be able to stop the execution by calling the
clearTimeout() method.
Syntax :
window.clearTimeout(timeoutVariable)
Cookies
Cookies let you store user information in web pages.
Cookies are data, stored in small text files, on your computer.
When a web server has sent a web page to a browser, the
connection is shut down, and the server forgets everything about the
user.
Cookies were invented to solve the problem "how to remember
information about the user":
When a user visits a web page, his name can be stored in a cookie.
Next time the user visits the page, the cookie "remembers" his name.
Cookies are saved in name-value pairs.
When a browser request a web page from a server, cookies
belonging to the page is added to the request. This way the server
gets the necessary data to "remember" information about users.
27
4/27/2015
Cookies, cont…
JavaScript can create, read, and delete cookies
with the document.cookie property, like :
document.cookie="username=John Doe";
You can also add an expiry date (in UTC time). By
default, the cookie is deleted when the browser is
closed. Example :
document.cookie="username=John Doe; expires=Thu,
18 Dec 2013 12:00:00 UTC";
With a path parameter, you can tell the browser
what path the cookie belongs to. By default, the
cookie belongs to the current page. Example :
document.cookie="username=John Doe; expires=Thu,
18 Dec 2013 12:00:00 UTC; path=/";
28