• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Opening Pages in a New Window
2
3
4The **Web** component provides the capability of opening pages in a new window. You can call [multiWindowAccess()](../reference/arkui-ts/ts-basic-components-web.md#multiwindowaccess9) to specify whether to allow a web page to be opened in a new window. When a new window is opened in the **Web** component, the application will receive a window opening event through [onWindowNew()](../reference/arkui-ts/ts-basic-components-web.md#onwindownew9). You need to add the code for processing the window opening request in the event callback.
5
6
7> **NOTE**
8>
9> If you do not want to open a new window in [onWindowNew()](../reference/arkui-ts/ts-basic-components-web.md#onwindownew9), set the return value of [ControllerHandler.setWebController()](../reference/arkui-ts/ts-basic-components-web.md#onwindownew9) to **null**.
10
11
12In the following example, when a user clicks the **Open Page in New Window** button, the application receives a window opening event in the [onWindowNew()](../reference/arkui-ts/ts-basic-components-web.md#onwindownew9) callback.
13
14
15- Application code:
16
17  For details about how to create a window, see [Web Development Examples] (https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Web/Browser).
18
19  ```ts
20  // xxx.ets
21  import web_webview from '@ohos.web.webview';
22  @Entry
23  @Component
24  struct WebComponent {
25    controller: web_webview.WebviewController = new web_webview.WebviewController();
26    build() {
27      Column() {
28        Web({ src:$rawfile("window.html"), controller: this.controller })
29        .multiWindowAccess(true)
30        .onWindowNew((event) => {
31          console.info("onWindowNew...");
32          var popController: web_webview.WebviewController = new web_webview.WebviewController();
33          // Create a window, associate it with popController, and have popController returned to the Web component. If you do not need to open a new window, set the return value to event.handler.setWebController(null).
34          event.handler.setWebController(popController);
35        })
36      }
37    }
38  }
39  ```
40
41
42- Code of the **window.html** page:
43
44  ```html
45  <!DOCTYPE html>
46  <html>
47  <head>
48      <meta charset="utf-8">
49      <title>WindowEvent</title>
50  </head>
51
52  <body>
53  <input type="button" value="Open Page in New Window" onclick="OpenNewWindow()">
54  <script type="text/javascript">
55      function OpenNewWindow()
56      {
57          let openedWindow = window.open("about:blank", "", "location=no,status=no,scrollvars=no");
58          if (openedWindow) {
59              openedWindow.document.body.write("<p>This is my window</p>");
60          } else {
61              log.innerHTML = "window.open failed";
62          }
63      }
64  </script>
65  </body>
66  </html>
67  ```
68