美食家大橙子 发表于 3 天前

uniapp webview web-view组件监听网页变革 url变革 与网页通讯

uniapp webview web-view组件监听网页变革 url变革 与网页通讯

需求是在app页面内嵌套自己的h5 或者别人的h5 切换页面时候获取到网页的变革 url的变更
第一种方法就是利用 html5+的方法

//html部分
<web-view :src="webviewPath" class="webview" height="100%" :webview-styles="{width:'100%',height:'100%'}"
                        id="webview" ></web-view>
//js部分       
<script>
export default {
onReady() {
// 获取当前Webview窗口对象
                        const ws = plus.webview.currentWebview();
                        var currentWebview = this.$scope.$getAppWebview()
                        setTimeout(() => {
                        let wv = currentWebview.children();
                        _this.ws.loadURL(_this.webviewPath) //加载h5链接
                        // 监听窗口触屏事件
                                _this.ws.addEventListener('touchstart', function(e) {
                                        setTimeout(() => {
                                                let orderUrl = _this.ws.getURL() //页面链接
                                                const wsTitle = _this.ws.getTitle() //页面标题
                                                console.log(_this.ws.getTitle())
                                                console.log(orderUrl)
                                                console.log('touchstart');
                                        }, 100)

                                }, false);
                                _this.ws.onloaded = () => {
                                        let orderUrl = _this.ws.getURL()
                                        console.log('onloaded事件加载完成', orderUrl)
                                };
                        },1000)//如果是页面初始化调用时,需要延时一下
}
}
</script>
第二种方法就是利用 html5+的方法 和uniapp的 uni.webview.js

利用uni.webview.js 就可以实现h5和app通讯了,想要h5的任何数据都用通过 uni.webView.postMessage来通报给
app
//html部分
<web-view :src="webviewPath" class="webview" height="100%" :webview-styles="{width:'100%',height:'100%'}"
                        id="webview" @message="receiveMsg"></web-view>
        methods: {
                        wsEvalJs() {
                                // 在webview加载完成后,注入JavaScript代码
                                this.ws.evalJSSync(`
                                     var script = document.createElement('script');
                                     script.src = 'https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.2.js';
                                     document.body.appendChild(script);
                                                       document.addEventListener("UniAppJSBridgeReady", function() {
                                                           console.log("初始化uniapp的API成功");
                                                               uni.webView.postMessage({ data: { type: "href",href:window.location.href,msg:"获取页面url" } });
                                                       });
                                      `);
                        },
                                //接收wenview h5的消息
                        receiveMsg(option){
                                console.log('接收到的消息参数'+JSON.stringify(option))
                               
                        },
                        }

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: uniapp webview web-view组件监听网页变革 url变革 与网页通讯