Tauri Core API - v2.6.0
    Preparing search index...

    Class Webview

    Create new webview or get a handle to an existing one.

    Webviews are identified by a label a unique identifier that can be used to reference it later. It may only contain alphanumeric characters a-zA-Z plus the following special characters -, /, : and _.

    import { Window } from "@tauri-apps/api/window"
    import { Webview } from "@tauri-apps/api/webview"

    const appWindow = new Window('uniqueLabel');

    appWindow.once('tauri://created', async function () {
    // `new Webview` Should be called after the window is successfully created,
    // or webview may not be attached to the window since window is not created yet.

    // loading embedded asset:
    const webview = new Webview(appWindow, 'theUniqueLabel', {
    url: 'path/to/page.html',

    // create a webview with specific logical position and size
    x: 0,
    y: 0,
    width: 800,
    height: 600,
    });
    // alternatively, load a remote URL:
    const webview = new Webview(appWindow, 'theUniqueLabel', {
    url: 'https://github.com/tauri-apps/tauri',

    // create a webview with specific logical position and size
    x: 0,
    y: 0,
    width: 800,
    height: 600,
    });

    webview.once('tauri://created', function () {
    // webview successfully created
    });
    webview.once('tauri://error', function (e) {
    // an error happened creating the webview
    });


    // emit an event to the backend
    await webview.emit("some-event", "data");
    // listen to an event from the backend
    const unlisten = await webview.listen("event-name", e => { });
    unlisten();
    });

    2.0.0

    Hierarchy (View Summary)

    Index

    Constructors

    • Creates a new Webview.

      Parameters

      • window: Window

        the window to add this webview to.

      • label: string

        The unique webview label. Must be alphanumeric: a-zA-Z-/:_.

      • options: WebviewOptions

      Returns Webview

      The Webview instance to communicate with the webview.

      import { Window } from '@tauri-apps/api/window'
      import { Webview } from '@tauri-apps/api/webview'
      const appWindow = new Window('my-label')

      appWindow.once('tauri://created', async function() {
      const webview = new Webview(appWindow, 'my-label', {
      url: 'https://github.com/tauri-apps/tauri',

      // create a webview with specific logical position and size
      x: 0,
      y: 0,
      width: 800,
      height: 600,
      });

      webview.once('tauri://created', function () {
      // webview successfully created
      });
      webview.once('tauri://error', function (e) {
      // an error happened creating the webview
      });
      });

    Properties

    label: string

    The webview label. It is a unique identifier for the webview, can be used to reference it later.

    window: Window

    The window hosting this webview.

    listeners: Record<string, EventCallback<any>[]>

    Local event listeners.

    Methods

    • Gets the Webview for the webview associated with the given label.

      Parameters

      • label: string

        The webview label.

      Returns Promise<null | Webview>

      The Webview instance to communicate with the webview or null if the webview doesn't exist.

      import { Webview } from '@tauri-apps/api/webview';
      const mainWebview = Webview.getByLabel('main');
    • Listen to an emitted event on this webview.

      Type Parameters

      • T

      Parameters

      • event: EventName

        Event name. Must include only alphanumeric characters, -, /, : and _.

      • handler: EventCallback<T>

        Event handler.

      Returns Promise<UnlistenFn>

      A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      const unlisten = await getCurrentWebview().listen<string>('state-changed', (event) => {
      console.log(`Got error: ${payload}`);
      });

      // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
      unlisten();
    • Listen to an emitted event on this webview only once.

      Type Parameters

      • T

      Parameters

      • event: EventName

        Event name. Must include only alphanumeric characters, -, /, : and _.

      • handler: EventCallback<T>

        Event handler.

      Returns Promise<UnlistenFn>

      A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      const unlisten = await getCurrent().once<null>('initialized', (event) => {
      console.log(`Webview initialized!`);
      });

      // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
      unlisten();
    • Emits an event to all targets.

      Type Parameters

      • T

      Parameters

      • event: string

        Event name. Must include only alphanumeric characters, -, /, : and _.

      • Optionalpayload: T

        Event payload.

      Returns Promise<void>

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().emit('webview-loaded', { loggedIn: true, token: 'authToken' });
    • Emits an event to all targets matching the given target.

      Type Parameters

      • T

      Parameters

      • target: string | EventTarget

        Label of the target Window/Webview/WebviewWindow or raw EventTarget object.

      • event: string

        Event name. Must include only alphanumeric characters, -, /, : and _.

      • Optionalpayload: T

        Event payload.

      Returns Promise<void>

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().emitTo('main', 'webview-loaded', { loggedIn: true, token: 'authToken' });
    • The position of the top-left hand corner of the webview's client area relative to the top-left hand corner of the desktop.

      Returns Promise<dpi.PhysicalPosition>

      The webview's position.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      const position = await getCurrentWebview().position();
    • The physical size of the webview's client area. The client area is the content of the webview, excluding the title bar and borders.

      Returns Promise<dpi.PhysicalSize>

      The webview's size.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      const size = await getCurrentWebview().size();
    • Closes the webview.

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().close();
    • Bring the webview to front and focus.

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().setFocus();
    • Sets whether the webview should automatically grow and shrink its size and position when the parent window resizes.

      Parameters

      • autoResize: boolean

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().setAutoResize(true);
    • Hide the webview.

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().hide();
    • Show the webview.

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().show();
    • Set webview zoom level.

      Parameters

      • scaleFactor: number

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().setZoom(1.5);
    • Moves this webview to the given label.

      Parameters

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().reparent('other-window');
    • Clears all browsing data for this webview.

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      import { getCurrentWebview } from '@tauri-apps/api/webview';
      await getCurrentWebview().clearAllBrowsingData();
    • Specify the webview background color.

      • macOS / iOS: Not implemented.
      • Windows:
        • On Windows 7, transparency is not supported and the alpha value will be ignored.
        • On Windows higher than 7: translucent colors are not supported so any alpha value other than 0 will be replaced by 255

      Parameters

      Returns Promise<void>

      A promise indicating the success or failure of the operation.

      2.1.0

    • Listen to a file drop event. The listener is triggered when the user hovers the selected files on the webview, drops the files or cancels the operation.

      Parameters

      Returns Promise<UnlistenFn>

      A promise resolving to a function to unlisten to the event. Note that removing the listener is required if your listener goes out of scope e.g. the component is unmounted.

      import { getCurrentWebview } from "@tauri-apps/api/webview";
      const unlisten = await getCurrentWebview().onDragDropEvent((event) => {
      if (event.payload.type === 'over') {
      console.log('User hovering', event.payload.position);
      } else if (event.payload.type === 'drop') {
      console.log('User dropped', event.payload.paths);
      } else {
      console.log('File drop cancelled');
      }
      });

      // you need to call unlisten if your handler goes out of scope e.g. the component is unmounted
      unlisten();

      When the debugger panel is open, the drop position of this event may be inaccurate due to a known limitation. To retrieve the correct drop position, please detach the debugger.