> Markdown version of [Browser Access](https://vaadin.com/docs/v24/flow/advanced/browser-access). Section index: [llms.txt](https://vaadin.com/docs/v24/flow/llms.txt)

# Accessing the Browser Page

Besides using the `Element` API to manipulate the DOM in the browser, you can also interact with the browser page by adding style sheets, executing JavaScript, and receiving browser resize events.

These features are available through the `Page` object. You can access the page of the current request with `UI.getCurrent().getPage()`.

## <a id="getting-the-window-location-url"></a>Getting the Window Location URL

Use the `Page.fetchCurrentURL()` method to fetch the current URL from the browser.

For example:

Getting the browser URL

```java
UI.getCurrent().getPage().fetchCurrentURL(currentUrl -> {
    // This is your own method that you may do something with the url.
    // Note that this method runs asynchronously
    storeCurrentURL(currentUrl);
});
```

## <a id="accessing-browser-information"></a>Accessing Browser Information

During the initial HTTP request, Vaadin reads the user-agent information supplied by the browser and stores them in a `WebBrowser` instance, which is accessible from the current `VaadinSession`.

Here’s an example how to get the `WebBrowser` instance and access some of the user-agent information:

```java
WebBrowser browser = VaadinSession.getCurrent().getBrowser();
// Print the raw user-agent string
System.out.println(browser.getBrowserApplication());
// Print some parsed browser details
System.out.println("Is Windows: " + browser.isWindows());
System.out.println("Is Chrome: " + browser.isChrome());
System.out.println("Major version: " + browser.getBrowserMajorVersion());
```

## <a id="getting-the-extended-client-side-details"></a>Getting the Extended Client-Side Details

Use `retrieveExtendedClientDetails()` in the current `Page` object to retrieve extended client-side details. The method accepts a callback that, when called after client response, gets an `ExtendedClientDetails` object with various client-side data.

For example, the following can be used to get the screen width:

Getting the screen width

```java
UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {
    // This is your own method that you may do something with the screen width.
    // Note that this method runs asynchronously
    handleScreenWidth(details.getScreenWidth());
});
```

## <a id="executing-javascript-in-the-browser"></a>Executing JavaScript in the Browser

You can use server-side Java to execute JavaScript snippets in the browser. You can also pass parameters to the executed script as variables named `$0`, `$1`, and so on. Vaadin automatically serializes and escapes the parameter values.

You can execute JavaScript in the browser and pass parameters as follows:

```java
public static void logElementSize(String name,
        Element element) {
    Page page = UI.getCurrent().getPage();

    page.executeJs(
            "console.log($0 + ' size:', "
            + "$1.offsetWidth, $1.offsetHeight)",
            name, element);
}
```

The supported parameter types are: `String`, `Boolean`, `Integer`, `Double`, `JsonValue`, and `Element`.

The script is executed after the DOM tree has been updated based on server-side changes. The parameter value is `null` for a parameter of type `Element` that isn’t attached after the update (according to the server-side component structure).

The script is executed asynchronously, so you can’t directly pass values back to the server. Instead, you can use the returned `PendingJavaScriptResult` instance to add a callback that’s called when the result is available.

## <a id="browser-window-resize-events"></a>Browser Window Resize Events

The `Page` class allows you to register a listener for events that affect the web page and the browser window in which the Vaadin UI lives. The `Page` instance corresponding to a given `UI` is accessed by the `getPage()` method of the `UI`.

You can get the browser window size by adding a resize listener, as follows:

```java
Page page = UI.getCurrent().getPage();
var listener = page.addBrowserWindowResizeListener(
        event -> Notification.show("Window width="
                + event.getWidth()
                + ", height=" + event.getHeight()));
```

> **Note:** When using `BrowserWindowResizeListener` you need to unregister it when you do not need to listen the events anymore in order to avoid memory leaks. For example you can do this when component detaches.

```java
@Override
protected void onDetach(DetachEvent detachEvent) {
    // Listener needs to be eventually removed in order to avoid resource leak
    listener.remove();
    super.onDetach(detachEvent);
}
```

`0DF2CC14-4401-49E4-B97D-920CAFEAAF8D`
