提交 8bfecf5e 编写于 作者: L lixiang

add onAlert&onBeforeUnload&onConfirm&onPrompt to load web pages

Signed-off-by: Nlixiang <lixiang380@huawei.com>
上级 2038f7a0
......@@ -4250,7 +4250,7 @@ static saveCookieAsync(callback: AsyncCallback\<void>): void
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------- | ---- | :------------------------------------------------- |
| callback | AsyncCallback\<void> | 是 | 返回cookie是否成功保存的布尔值作为回调函数的入参。 |
| callback | AsyncCallback\<void> | 是 | callback回调,用于获取cookie是否成功保存。 |
**示例:**
......
......@@ -718,8 +718,6 @@ textZoomAtio(textZoomAtio: number)
```ts
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
......@@ -1420,8 +1418,10 @@ onAlert(callback: (event?: { url: string; message: string; result: JsResult }) =
controller: web_webview.WebviewController = new web_webview.WebviewController()
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
Web({ src: $rawfile("xxx.html"), controller: this.controller })
.onAlert((event) => {
console.log("event.url:" + event.url)
console.log("event.message:" + event.message)
AlertDialog.show({
title: 'onAlert',
message: 'text',
......@@ -1448,6 +1448,25 @@ onAlert(callback: (event?: { url: string; message: string; result: JsResult }) =
}
```
```
<!--xxx.html-->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
</head>
<body>
<h1>WebView onAlert Demo</h1>
<button onclick="myFunction()">Click here</button>
<script>
function myFunction() {
alert("Hello World");
}
</script>
</body>
</html>
```
### onBeforeUnload
onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)
......@@ -1481,7 +1500,7 @@ onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResu
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
Web({ src: $rawfile("xxx.html"), controller: this.controller })
.onBeforeUnload((event) => {
console.log("event.url:" + event.url)
console.log("event.message:" + event.message)
......@@ -1511,6 +1530,25 @@ onBeforeUnload(callback: (event?: { url: string; message: string; result: JsResu
}
```
```
<!--xxx.html-->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
</head>
<body onbeforeunload="return myFunction()">
<h1>WebView onBeforeUnload Demo</h1>
<a href="https://www.example.com">Click here</a>
<script>
function myFunction() {
return "onBeforeUnload Event";
}
</script>
</body>
</html>
```
### onConfirm
onConfirm(callback: (event?: { url: string; message: string; result: JsResult }) => boolean)
......@@ -1544,11 +1582,10 @@ onConfirm(callback: (event?: { url: string; message: string; result: JsResult })
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
Web({ src: $rawfile("xxx.html"), controller: this.controller })
.onConfirm((event) => {
console.log("event.url:" + event.url)
console.log("event.message:" + event.message)
console.log("event.result:" + event.result)
AlertDialog.show({
title: 'onConfirm',
message: 'text',
......@@ -1575,6 +1612,34 @@ onConfirm(callback: (event?: { url: string; message: string; result: JsResult })
}
```
```
<!--xxx.html-->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
</head>
<body>
<h1>WebView onConfirm Demo</h1>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
<script>
function myFunction() {
let x;
let r = confirm("click button!");
if (r == true) {
x = "ok";
} else {
x = "cancel";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
```
### onPrompt<sup>9+</sup>
onPrompt(callback: (event?: { url: string; message: string; value: string; result: JsResult }) => boolean)
......@@ -1606,7 +1671,7 @@ onPrompt(callback: (event?: { url: string; message: string; value: string; resul
build() {
Column() {
Web({ src: 'www.example.com', controller: this.controller })
Web({ src: $rawfile("xxx.html"), controller: this.controller })
.onPrompt((event) => {
console.log("url:" + event.url)
console.log("message:" + event.message)
......@@ -1623,7 +1688,7 @@ onPrompt(callback: (event?: { url: string; message: string; value: string; resul
secondaryButton: {
value: 'ok',
action: () => {
event.result.handleConfirm()
event.result.handlePromptConfirm(event.value)
}
},
cancel: () => {
......@@ -1637,6 +1702,30 @@ onPrompt(callback: (event?: { url: string; message: string; value: string; resul
}
```
```
<!--xxx.html-->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
</head>
<body>
<h1>WebView onPrompt Demo</h1>
<button onclick="myFunction()">Click here</button>
<p id="demo"></p>
<script>
function myFunction() {
let message = prompt("Message info", "Hello World");
if (message != null && message != "") {
document.getElementById("demo").innerHTML = message;
}
}
</script>
</body>
</html>
```
### onConsole
onConsole(callback: (event?: { message: ConsoleMessage }) => boolean)
......@@ -1690,6 +1779,7 @@ onDownloadStart(callback: (event?: { url: string, userAgent: string, contentDisp
| 参数名 | 参数类型 | 参数描述 |
| ------------------ | ------------- | ----------------------------------- |
| url | string | 文件下载的URL。 |
| userAgent | string | 用于下载的用户代理。 |
| contentDisposition | string | 服务器返回的 Content-Disposition响应头,可能为空。 |
| mimetype | string | 服务器返回内容媒体类型(MIME)信息。 |
| contentLength | contentLength | 服务器返回文件的长度。 |
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册