FSD-EXP-7(JavaScript Pre-defined and User-defined Objects)
FSD-EXP-7(JavaScript Pre-defined and User-defined Objects)
The document object contains the various properties and methods you can use to get details about
HTML elements and customize them.
The JavaScript document object is a property of the window object. It can be accessed
using window.document syntax. It can also be accessed without prefixing window object.
The JavaScript Document Object represents the entire HTML document, and it comes with several
properties that allow us to interact with and manipulate the document. Some common Document
object properties are as follows –
In the example below, we use the document.title property to access the property odd the document.
<html>
<head>
<title> JavaScript - DOM Object </title>
</head>
<body>
<div id = "output">The title of the document is: </div>
<script>
document.getElementById("output").innerHTML += document.title;
</script>
</body>
</html>
OutPut:
The title of the document is: JavaScript - DOM Object
Example: Accessing the document URL
In the example below, we have used the document.URL property to access the current URL of the
page.
<html>
<head>
</head>
<body>
<script>
document.getElementById("output").innerHTML += document.URL;
</script>
</body>
</html>
OutPut:
The JavaScript Document Object provides us with various methods that allow us to interact with and
manipulate the HTML document. Some common Document object methods are as follows –
These methods enable us to dynamically manipulate the structure and content of the HTML
document using JavaScript.
In the example below, we use document.getElementById() method to access the DIV element with id
"output" and then use the innerHTML property of the HTML element to display a message.
<html>
<body>
<script>
document.getElementById("result").innerHTML +=
"Hello User! You have accessed the DIV element using its id.";
</script>
</body>
</html>
OutPut:
Hello User! You have accessed the DIV element using its id.
In the example below, we use document.addEventListener() method to add a mouseover event to the
document.
<html>
<body>
<div id = "result">
</div>
<script>
document.addEventListener('mouseover', function () {
});
</script>
</body>
</html>
OutPut:
The 'window' object contains the various properties, returning the status and information about the
current window.
Below, we have covered all properties of the 'window' object with a description. You may use the
'window' as a reference to access these properties.
customElements It is used to define and access the custom elements in the browser window.
devicePixelRati
It returns the physical pixel ratio of the device divided by CSS pixel ratio.
o
document It is used to access the HTML document opened in the current window.
It is used to get the window items like iframes, which are opened in the
frames
current window.
It returns the inner height of the window without including the scroll bar,
innerHeight
toolbar, etc.
It returns the inner width of the window without including the scroll bar,
innerWidth
toolbar, etc.
pageXOffset It returns the number of pixels you have scrolled the web page horizontally.
pageYOffset It returns the number of pixels you have scrolled the web page vertically.
parent It contains the reference to the parent window of the current window.
sessionStorage It lets you access the 'sessionStorage' object of the current window.
visualViewPort It returns the object containing the viewport of the current window.
The outerHeight property of the window object returns the window's height, and the outerWidth
property of the window object returns the window's width.
Example
In the code below, we used the outerHeight and outerWidth property to get the dimensions of the
window. You can change the size of the window and observe changes in the value of these
properties.
<html>
<body>
<script>
document.getElementById("output1").innerHTML += outerWidth;
document.getElementById("output2").innerHTML += outerHeight;
</script>
</body>
</html>
OutPut:
The window screenLeft property returns the left position of the current window.
Example
In the output of the below code, you can see the left position of the current window in pixels.
<html>
<body>
<script>
</script>
</body>
</html>
Output
Your browser window is left by: 0 px.