Web组件如何判断网址是否加载成功

Web组件如何判断网址是否加载成功

HarmonyOS
2024-03-17 17:33:32
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
失望的满天星
  • 方案一:在设备无网络时,网页加载会发生错误,可以触发Web组件的onErrorReceive回调函数。
  • 方案二:在网页加载资源遇到HTTP错误(响应码>=400)时,可以触发Web组件的onHttpErrorReceive回调函数。

开发者可以根据不同的场景在对应的回调函数中进行相应的处理。

onErrorReceive:

// xxx.ets 
import { webview } from '@kit.ArkWeb'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: webview.WebviewController = new webview.WebviewController(); 
 
  build() { 
    Column() { 
      Web({ src: 'www.baidu.com', controller: this.controller }) 
        .onErrorReceive((event) => { 
          if (event) { 
            console.log('getErrorInfo:' + event.error.getErrorInfo()); 
            console.log('getErrorCode:' + event.error.getErrorCode()); 
            console.log('url:' + event.request.getRequestUrl()); 
            console.log('isMainFrame:' + event.request.isMainFrame()); 
            console.log('isRedirect:' + event.request.isRedirect()); 
            console.log('isRequestGesture:' + event.request.isRequestGesture()); 
            console.log('getRequestHeader_headerKey:' + event.request.getRequestHeader().toString()); 
            let result = event.request.getRequestHeader(); 
            console.log('The request header result size is ' + result.length); 
            for (let i of result) { 
              console.log('The request header key is : ' + i.headerKey + ', value is : ' + i.headerValue); 
            } 
          } 
        }) 
    } 
  } 
}

onHttpErrorReceive:

// xxx.ets 
import { webview } from '@kit.ArkWeb'; 
 
@Entry 
@Component 
struct WebComponent { 
  controller: webview.WebviewController = new webview.WebviewController(); 
 
  build() { 
    Column() { 
      Web({ src: 'www.example.com', controller: this.controller }) 
        .onHttpErrorReceive((event) => { 
          if (event) { 
            console.log('url:' + event.request.getRequestUrl()); 
            console.log('isMainFrame:' + event.request.isMainFrame()); 
            console.log('isRedirect:' + event.request.isRedirect()); 
            console.log('isRequestGesture:' + event.request.isRequestGesture()); 
            console.log('getResponseData:' + event.response.getResponseData()); 
            console.log('getResponseEncoding:' + event.response.getResponseEncoding()); 
            console.log('getResponseMimeType:' + event.response.getResponseMimeType()); 
            console.log('getResponseCode:' + event.response.getResponseCode()); 
            console.log('getReasonMessage:' + event.response.getReasonMessage()); 
            let result = event.request.getRequestHeader(); 
            console.log('The request header result size is ' + result.length); 
            for (let i of result) { 
              console.log('The request header key is : ' + i.headerKey + ' , value is : ' + i.headerValue); 
            } 
            let resph = event.response.getResponseHeader(); 
            console.log('The response header result size is ' + resph.length); 
            for (let i of resph) { 
              console.log('The response header key is : ' + i.headerKey + ' , value is : ' + i.headerValue); 
            } 
          } 
        }) 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-03-18 21:11:05
相关问题
如何判断Web组件是否全屏
2045浏览 • 1回复 待解决
Web如何判断是否发生重定向
1001浏览 • 1回复 待解决
使用web无法打开HarmonyOS网址
261浏览 • 1回复 待解决
HarmonyOS Web组件UserAgent判断方法
24浏览 • 1回复 待解决
HarmonyOS web组件如何加载本地字库?
247浏览 • 1回复 待解决
HarmonyOS web组件 加载web页面异常
45浏览 • 1回复 待解决
HarmonyOS web组件滚动方向判断
31浏览 • 1回复 待解决
HarmonyOS Web组件加载太慢
32浏览 • 1回复 待解决
如何禁止web组件自动加载图片?
780浏览 • 1回复 待解决
HarmonyOS web组件加载pdf问题
659浏览 • 1回复 待解决
HarmonyOS Web组件HTTP图片加载如何放行
486浏览 • 1回复 待解决
Web组件中的预加载如何实现?
1334浏览 • 1回复 待解决
HarmonyOS Web组件如何加载html字符串
651浏览 • 1回复 待解决