Window Object in JavaScript. Code Examples

Window Object in JavaScript. Code Examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

All objects through which JavaScript interacts with the browser are described by such a concept as the Browser Object Model

BOM tree

The window object represents the browser. This object in turn includes a number of other objects, in particular the document object, which represents a single web page that is displayed in the browser.

Window is a global object, so it is not necessary to use its name when accessing its properties and methods.

For example, window has an alert() method that displays a message box:

    window.alert("Hello");
    alert('Hello');

But since this object is global, this imposes some restrictions. For example:

    var alert = function (message) {

        document.write("Message: " + message);
    };
    window.alert("Hello");
All global variables or functions declared in the program are automatically added to the window object.

And since the name of the new function will be the same as the name of the alert() method, this method will be overridden in the window object by the new function.

And if we declare some global variable in the program, then it is available to us as a property in the window object:

    var message = "hello";
    document.write(window.message)//hello

Window management

For user interaction, the window object defines a number of methods that allow you to create dialog windows.

The alert() method displays a window with a message:

    alert("hello");

The confirm() method displays a message box in which the user must confirm the operation of the two OK and Cancel buttons. Depending on the user’s choice, the method returns true (if the user clicked OK) or false (if the user clicked the Cancel button):

    var result = confirm("Are you over 18?");
    if (result === true)
        document.write("The program has ended");
    else
        document.write("The program continues to run");

The prompt() method allows you to use a dialog window to prompt the user for some input. This method
returns the value entered by the user:

    var age = prompt("Enter your age:");
    document.write(age);
If the user refuses to enter a value and clicks the cancel button, the method will return null.

Opening, closing and positioning windows

The window object also provides a number of methods for managing browser windows. For example, the open() method opens a specific resource in a new browser window or tab.

It should be borne in mind that it is better to perform such an action on the basis of a user action, for example, by clicking on a button, because that browsers may otherwise block such windows.

For example, let’s define the following page:

<head>
        <title>Test</title>
</head>

<body>
    <button onclick="openWindow()">Click</button>
    <script>
        function openWindow() {
            window.open("https://site.com");
        }
    </script>
</body>

Here, a button is defined on the web page – the button element. The button has the onclick attribute set, which points to a JavaScript function that will be executed when the button is clicked.

The open() method takes a number of parameters:

  • open(url) – resource path
  • open(url, _self) – the page opens in the current tab
  • open(url, _blank) – page opens in a new tab or window
  • open(“url”, “_blank”, “popup=yes”) – popup: specifies whether the page will open in a separate popup window. This can take values such as yes, 1 or true
  • open(“url”, “_blank”, “width=600,height=400”) – width / innerWidth, height / innerHeight width and height of the window in pixels
  • open(“url”, “_blank”, left=500,top=200″) – left / screenX, top / screenY coordinates relative to the origin of the x and y screen axis

That the function returns a link to the window, and with the help of this link we can control the window.
The close() method closes the window. For example, let’s open a new window and close it after 5 seconds:

    function openWindow() {
        const popup = window.open("url", "_blank", "width=600,height=400");
        setTimeout(() => popup.close(), 5000);
    }

The moveTo() method allows you to move the window to a new position:

    const popup = window.open("url", "_blank", "width=600,height=400");
    popup.moveTo(500, 400);

In this case, the window is moved to the position with coordinates x=500, y=400 relative to the upper left corner of the screen.

Read also:

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation