代码拉取完成,页面将自动刷新
【需求价值】:
loadData可用来加载html字符串,使用中含有特殊字符的字符串无法加载
【需求描述】:
loadData支持加载含特殊字符的网页字符串
【方案描述】:
import webview from '@ohos.web.webview'
@Entry
@Component
struct WebComponent {
controller: web_webview.WebviewController = new webview.WebviewController();
updataContent: string = '<body><div><image src=file:///data/storage/el1/bundle/entry/resources/rawfile/xxx.png alt="image -- end" width="500" height="250"></image></div></body>'
build() {
Column() {
Button('loadData')
.onClick(() => {
try {
this.controller.loadData(this.updataContent, "text/html", "UTF-8", " ", " ");
} catch (error) {
console.error(`ErrorCode: ${error.code}, Message: ${error.message}`);
}
})
Web({ src: 'www.example.com', controller: this.controller })
}
}
}
上述代码可成功加载,但是如果在updataContent中添加#则显示空白,例如updataContent = '<body><div><image src=file:///data/storage/el1/bundle/entry/resources/rawfile/xxx.png# alt="image -- end" width="500" height="250"></image></div></body>'
推测原因仅对updataContent做encodeURI,该方法不会对#进行编码
将this.controller.loadData(this.updataContent, "text/html", "UTF-8", " ", " ");
改成this.controller.loadData(encodeURIComponent(this.updataContent), "text/html", "UTF-8", " ", " ");
则加载正常,改成this.controller.loadData(encodeURI(this.updataContent), "text/html", "UTF-8", " ", " ");
显示空白
【配套文档】:
【验收标准】:
loadData支持加载含特殊字符的网页字符串
登录 后才可以发表评论