W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Web組件提供了在新窗口打開(kāi)頁(yè)面的能力,開(kāi)發(fā)者可以通過(guò)multiWindowAccess()接口來(lái)設(shè)置是否允許網(wǎng)頁(yè)在新窗口打開(kāi)。當(dāng)有新窗口打開(kāi)時(shí),應(yīng)用側(cè)會(huì)在onWindowNew()接口中收到Web組件新窗口事件,開(kāi)發(fā)者需要在此接口事件中,新建窗口來(lái)處理Web組件窗口請(qǐng)求。
如下面的本地示例,當(dāng)用戶點(diǎn)擊“新窗口中打開(kāi)網(wǎng)頁(yè)”按鈕時(shí),應(yīng)用側(cè)會(huì)在onWindowNew()接口中收到Web組件新窗口事件。
- // xxx.ets
- import web_webview from '@ohos.web.webview'
- //在同一page頁(yè)有兩個(gè)web組件。在WebComponent新開(kāi)窗口時(shí),會(huì)跳轉(zhuǎn)到NewWebViewComp。
- @CustomDialog
- struct NewWebViewComp {
- controller?: CustomDialogController
- webviewController1: web_webview.WebviewController = new web_webview.WebviewController()
- build() {
- Column() {
- Web({ src: "", controller: this.webviewController1 })
- .javaScriptAccess(true)
- .multiWindowAccess(false)
- .onWindowExit(()=> {
- console.info("NewWebViewComp onWindowExit")
- if (this.controller) {
- this.controller.close()
- }
- })
- }
- }
- }
- @Entry
- @Component
- struct WebComponent {
- controller: web_webview.WebviewController = new web_webview.WebviewController()
- dialogController: CustomDialogController | null = null
- build() {
- Column() {
- Web({ src:$rawfile("window.html"), controller: this.controller })
- .javaScriptAccess(true)
- //需要使能multiWindowAccess
- .multiWindowAccess(true)
- .onWindowNew((event) => {
- if (this.dialogController) {
- this.dialogController.close()
- }
- let popController:web_webview.WebviewController = new web_webview.WebviewController()
- this.dialogController = new CustomDialogController({
- builder: NewWebViewComp({webviewController1: popController})
- })
- this.dialogController.open()
- //將新窗口對(duì)應(yīng)WebviewController返回給Web內(nèi)核。
- //如果不需要打開(kāi)新窗口請(qǐng)調(diào)用event.handler.setWebController接口設(shè)置成null。
- //若不調(diào)用event.handler.setWebController接口,會(huì)造成render進(jìn)程阻塞。
- event.handler.setWebController(popController)
- })
- }
- }
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>WindowEvent</title>
- </head>
- <body>
- <input type="button" value="新窗口中打開(kāi)網(wǎng)頁(yè)" onclick="OpenNewWindow()">
- <script type="text/javascript">
- function OpenNewWindow()
- {
- let openedWindow = window.open("about:blank", "", "location=no,status=no,scrollvars=no");
- openedWindow.document.write("<p>這是我的窗口</p>");
- openedWindow.focus();
- }
- </script>
- </body>
- </html>
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: