Javascript Window - 28 Methods (Reference)

Tech:
Javascript
Since:
1 year ago
Views:
3

Window Object in Javascript is a global object that is available in all web pages. It provides a way to access the browser window, its document, its location, and more.

console()

The console() method provides access to the browser's debugging console.

window.console.log("Some text");
console.log("Some text");

alert()

The alert() method displays an alert box with a message and an OK button.

window.alert("Some text");
alert("Some text");

confirm()

The confirm() method displays a modal dialog with an optional message and two buttons 'OK' and 'Cancel'.

window.confirm("some text");

prompt()

The prompt() method displays a dialog with an optional message prompting the user to input some text.

var x = window.prompt("Some heading", "some input value");

// window.prompt(message, default_val);

blur()

The blur() method removes focus from the current window.

window.blur();

focus()

The focus() method makes a request to bring the window to the front.

window.focus();

close()

The close() method closes the current window.

window.close();

stop()

The stop() method stops further resource loading in the current browsing context.

window.stop();

getComputedStyle()

The getComputedStyle() method returns an object containing the values of all CSS properties of an element.

var x = document.body;
var y = window.getComputedStyle(x);

getSelection()

The getSelection() method returns selected text object.

var x = window.getSelection();

moveBy()

The moveBy() method moves the current window by a specified amount. It can't move a window or tab that wasn’t created by Window.open() or when it’s in a window with more than one tab.

window.moveBy(10, -10);

moveTo()

The moveTo() method moves the current window by a specified amount. It can't move a window or tab that wasn’t created by Window.open() or when it’s in a window with more than one tab.

window.moveTo(0, 0);

open()

The open() method loads the specified resource into the new/existing browsing context with the specified name. If no name, then a new browsing context is opened in a new tab/window and the specified resource is loaded.

var Features =
  "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
window.open("www.fallinfo.com", "FALLINFO", Features);

// var window = window.open(url, windowName, [windowFeatures]);

The print() method opens the Print Dialog to print the current document.

window.print();

resizeBy()

The resizeBy() method resizes the current window by a specified amount. To make the window resizable open it with the "resizable" feature.

window.resizeBy(-200, -200);

// window.resizeBy(xDelta, yDelta)

resizeTo()

The resizeTo() method resizes the current window by a specified amount. It's not possible to resize a window or tab that wasn’t created by window.open() when the window has multiple tabs.

window.resizeTo(10, 10);

// window.resizeTo(width, height)

scroll()

The scroll() method scrolls the window to a particular place in the document.

window.scroll(0, 100);

window.scroll({
  top: 100,
  left: 100,
  behavior: "smooth"
});

// no safari support

scrollBy()

The scrollBy() method scrolls the document in the window by the given amount.

window.scrollBy({
  top: 100,
  left: 100,
  behavior: "smooth"
});

// To scroll down one page:
window.scrollBy(0, window.innerHeight);

// To scroll up:
window.scrollBy(0, -window.innerHeight);

// no safari support

scrollTo()

The scrollTo() method scrolls to a particular set of coordinates in the document.

window.scrollTo(0, 1000);

window.scrollTo({
  top: 100,
  left: 100,
  behavior: "smooth"
});

// no safari support

atob()

The atob() method decodes a string of data which has been encoded using Base64 encoding / btoa().

var x = window.btoa("some_text"); // encode a string
var y = window.atob(x); // decode the string

btoa()

The btoa() method creates a Base64-encoded ASCII string from a binary string.

var x = window.btoa("some_text"); // encode a string
var y = window.atob(x); // decode the string

setTimeout()

The setTimeout() method schedules a function to execute in a given amount of time.

var x = window.setTimeout(function() {
  // code
}, 3000);

// setTimeout(function, milliseconds, param1, param2, ...)

clearTimeout()

The clearTimeout() method cancels the delayed execution set using.

var x = window.setTimeout(function() {
  // code
}, 3000);

clearTimeout(x);

setInterval()

The setInterval() method schedules a function to execute every time a given number of milliseconds elapses.

var x = setInterval(function() {
  // code
}, 3000);

// setInterval(function, milliseconds, param1, param2, ...)

clearInterval()

The clearInterval() method cancels the repeated execution set using.

var x = setInterval(function() {
  // code
}, 3000);

clearInterval(x);

fetch()

The fetch() method starts the process of fetching a resource from the network.

var x = fetch(resource [, init])

createImageBitmap()

The createImageBitmap() method accepts a variety of different image sources, and returns a Promise which resolves to an ImageBitmap.

var imageBitmapPromise = createImageBitmap(image[, options]);
var imageBitmapPromise = createImageBitmap(image, sx, sy, sw, sh[, options]);

queueMicrotask()

The queueMicrotask() method queues a microtask, which is a task that is run before the next event loop.

window.queueMicrotask(function);