9.the Browser Object Model
9.the Browser Object Model
document.getElementById("header");
Some of the most commonly used properties and methods of the window object
include:
<script type="text/javascript">
function msg(){
alert("Hello Alert Box");
}
</script>
<input type="button" value="click" onclick="msg()"/>
This code will display an alert box with the message "Hello, world!".
window.screen Or screen
Properties:
• screen.width
• screen.height
• screen.availWidth
• screen.availHeight
• screen.colorDepth
• screen.pixelDepth
screen.width Property: The screen.width property returns the user’s screen
width in pixels.
HTML screen.height Property: The screen.height property returns the users
screen height in pixels.
HTML screen.availWidth Property: The screen.availWidth property returns the
users screen width in pixels, excluding the interface features like the Windows
Taskbar.
HTML screen.availHeight Property: The screen.availHeight property returns the
users screen height in pixels excluding the interface features.
HTML screen.colorDepth Property: The screen.colorDepth property returns the
bits (number) to be used to display one color. Usually, 24-bit or 32-bit hardware
is used for color resolution. 24 bits = 16, 777, 216 different (True Colors) 32 bits =
4, 294, 967, 296 different (Deep Colors)
HTML screen.pixelDepth Property: The screen.pixelDepth property returns the
pixel depth of the screen.
Some examples:
• window.location.href returns the href (URL) of the current page
document.getElementById("demo").innerHTML =
"Page location is " + window.location.href;
document.getElementById("demo").innerHTML =
"Page hostname is " + window.location.hostname;
document.getElementById("demo").innerHTML =
"Page path is " + window.location.pathname;
The history object is the window property, so it can be accessed by: window.history
Example
<script>
function goBack(){
window.history.back()
}
</script>
Browser Cookies
The cookieEnabled property returns true if cookies are enabled, otherwise false:
<script>
document.getElementById("demo").innerHTML =
"cookiesEnabled is " + navigator.cookieEnabled;
</script>
The appCodeName property returns the application code name of the browser
Is Java Enabled?
window.document
Method Description
Document.write("string") writes the given string on the document. (Html output stream)
Document.writeln("string") writes the given string on the document with newline character
at the end.
getElementsByName() returns all the elements having the given name value.
getElementsByTagName() returns all the elements having the given tag name.
getElementsByClassName() returns all the elements having the given class name.
<script>
document.getElementById("demo").innerHTML = "Hello World!";
</ The most common way to access an HTML element is to use the id of the
element.
In the example above the getElementById method used id="demo" to find the
element.
script>
Method Description