Chapter 4 Cookies and Browser Data
Chapter 4 Cookies and Browser Data
var x = document.getElementById("demo");
x.innerHTML = "Browser inner window width: " + w + ", height: " + h + ".";
</script>
</body>
</html>
Other Window Methods:
• window.open() - The open() method opens a new browser window,
or a new tab, depending on your browser settings and the parameter
values.
• window.close() - close the current window
• window.moveTo() - move the current window
• window.resizeTo() - resize the current window
Opening a Window in new browser tab:
• Open "www.w3schools.com" in a new browser tab
<html>
<body>
<p>Click the button to open a new browser window.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
window.open("https://www.w3schools.com");
}
</script>
</body>
</html>
Opening a blank page in new Window:
• Open an about:blank page in a new window/tab:
<html>
<body>
<p>Click the button to open an about:blank page in a new browser
window that is 200px wide and 100px tall.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200,height=100");
}
</script>
</body>
</html>
Opening Multiple tabs:
• Open Multiple tabs:
<html>
<body>
<p>Click the button to open multiple tabs.</p>
<button onclick="myFunction()">Open Windows</button>
<script>
function myFunction() {
window.open("http://www.google.com/");
window.open("https://www.w3schools.com/");
}
</script>
</body>
</html>
Closing a Window:
<html>
<body>
<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<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>
Getting a focus to new Window:
The focus() method sets focus to the current window.
<html>
<body>
<p>Click the button to open a new window, and assure that the new window GETS
focus (send the new window to the front).</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myWindow = window.open("", "", "width=200,height=100");
myWindow.document.write("<p>A new window!</p>");
myWindow.focus();
}
</script>
</body>
</html>
Changing contents of a Window:
• The replace() method replaces the current document with a new one.
• The replace() removes the URL of the current document from the
document history, meaning that it is not possible to use the "back" button
to navigate back to the original document.
Ex:
<html>
<body>
<button onclick="myFunction()">Replace document</button>
<script>
function myFunction() {
location.replace("https://www.w3schools.com") }
</script>
</body>
</html>
Scrolling the page of a Window:
• The scrollTo() method scrolls the document to the specified coordinates.
• The scrollBy() method used to scroll a specified distance multiple times.
<html>
<head>
<style>
body { width: 5000px; }
</style>
</head>
<body>
<p>Click the button to scroll the document window to 500 pixels horizontally.</p>
<button onclick="scrollWin()">Click me to scroll horizontally!</button><br><br>
<script>
function scrollWin() { window.scrollTo(500, 0); }
</script>
</body>
</html>
Window Screen:
• The window.screen object contains information about the user's
screen.
• The window.screen object can be written without the window
prefix.
• Properties:
– screen.width
– screen.height
– screen.availWidth
– screen.availHeight
– screen.colorDepth: returns the number of bits used to display
one color.
– screen.pixelDepth: returns the pixel depth of the screen
Window Screen:
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Screen width is "
+ screen.width;
</script>
</body>
</html>
Window Location:
• The window.location object can be used to get the current page
address (URL) and to redirect the browser to a new page.
• The window.location object can be written without the window
prefix.
• Ex:
• window.location.href: returns the href (URL) of the current page
• window.location.hostname: returns the domain name of 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
Window Location:
<html>
<body>
<h2>JavaScript</h2>
<h3>The window.location object</h3>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"The full URL of this page is:<br>" + window.location.href;
</script>
</body>
</html>
Window Location:
<html>
<head>
<script>
function newDoc()
{
window.location.assign("https://www.w3schools.com")
}
</script>
</head>
<body>
<input type="button" value="Load new
document" onclick="newDoc()">
</body>
</html>
Window History:
• The window.history object contains the browsers history.
• The window.history object can be written without the window
prefix.
• To protect the privacy of the users, there are limitations to how
JavaScript can access this object.
• Methods:
• history.back() - same as clicking back in the browser
• history.forward() - same as clicking forward in the browser
Window Navigator:
• The window.navigator object contains information about the
visitor's browser.
• The window.navigator object can be written without the window
prefix.
• Properties:
navigator.cookieEnabled: returns true if cookies are enabled,
otherwise false
navigator.appName:returns the application name of the browser
navigator.appCodeName: returns application code name of
browser
navigator.product: returns product name of the browser engine
navigator.appVersion:returns version information about the
browser
Timers
• The window object allows execution of code at specified time
intervals.
• These time intervals are called timing events.
• The two key methods to use with JavaScript are:
setTimeout(function, milliseconds)
– Executes a function, after waiting a specified number of
milliseconds.
– setInterval(function, milliseconds)
– Same as setTimeout(), but repeats the execution of the function
continuously
The setTimeout() and setInterval() are both methods of the HTML
DOM Window object.
setTimeOut()
window.setTimeout(function, milliseconds);
• The window.setTimeout() method can be written without the
window prefix.
• The first parameter is a function to be executed.
window.clearTimeout(timeoutVariable)
• The window.clearTimeout() method can be written without
the window prefix.