Maharashtra's Most Trusted
Chapter 4 - Cookies & Browser Data
Part 2 Notes
Browser -:
he Browser
object in JavaScript represents the web browser and provides access to various properties and methods to interact with the browser environment. JavaScript itself runs in the browser and can manipulate web pages, handle events, interact with the Document Object Model (DOM), and more.
Though there is no specific object called Browser
in JavaScript, the browser environment offers several key objects that allow developers to interact with the browser and the web page:
1. Window
Object
The window
object is the global object in JavaScript that represents the browser window. It gives access to browser-level properties and methods.
Key properties and methods:
window.innerWidth
,window.innerHeight
: Get the browser window’s width and height.window.alert()
: Display an alert box.window.open()
: Open a new browser window.window.location
: Represents the current URL of the browser.
Window -:
The window
object in JavaScript represents the browser’s window or tab in which the script is running. It is the global object in the browser environment, meaning all global variables and functions defined in JavaScript are actually properties and methods of the window
object.
Opening & Closing Window -:
The window.open()
method is used to open a new browser window or tab. You can specify a URL, target, and various options (like the size and position of the new window).
Parameters:
- URL: The URL of the page to open. If left empty, a blank page (
about:blank
) is opened. - windowName: A name for the new window or tab. You can use this name later to refer to the window for actions like closing or reloading it. Common values are:
_blank
: Opens in a new tab (or new window if the browser is set to open in a new window)._self
: Opens in the same tab (similar to navigating the current page).- Custom name: A name you define, which can be used later to reference the window.
- windowFeatures (optional): A string that specifies the dimensions and settings of the new window. These can include width, height, scrollbars, and more.
Closing a Window
The window.close()
method is used to close the current window or a window that was opened via window.open()
. Note that modern browsers often block scripts from closing a window unless it was opened using JavaScript.
Giving window the focus -:
In JavaScript, you can give focus to a specific browser window or tab using the window.focus()
method. This brings the window to the front, making it the active window. This is useful when you open a new window and want to make sure the user can see it, or when you want to bring attention back to a particular window.
Changing the contents of window -:
If you’ve opened a new window using window.open()
, you can access and change the content of that new window using the window reference returned by window.open()
. You can either inject new HTML or modify existing HTML in the opened window.
SetInterval() & SetTimeout() in Java script -:
In JavaScript, setTimeout()
and setInterval()
are two key methods used for handling timing events. Both are part of the browser’s Web API and allow you to execute code after a delay or at regular intervals.
1. setTimeout()
Method
setTimeout()
is used to execute a function or a block of code once after a specified delay (in milliseconds).
function
: The function to execute after the delay.delay
: The number of milliseconds to wait before executing the function
setInterval()
Method
setInterval()
is used to repeatedly execute a function or a block of code at regular intervals. The function will continue to execute until it’s stopped using clearInterval()
.
Chapter 4 Assignment -------->
Explore Other Chapters