web-view的src只能帶一個參數src="…?a=1"
h5頁面
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i) == 'micromessenger') { //判斷是否是微信環境 //微信環境 wx.miniProgram.getEnv(function (res) { if (res.miniprogram) { // 小程序環境下邏輯 //小程序發送信息``` var info = { title: '11', //參數一 value: '22', //參數二 }; wx.miniProgram.postMessage({ data: info }); } else { window.location.href = 'https://www.baidu.com'; //非小程序環境下邏輯 } }) } else { //非微信環境邏輯 window.location.href = 'https://blog.csdn.net/qq_37235231/article/details/82053062'; }
小程序頁面
index.wxml頁面
<web-view src="{{webUrl}}" bindmessage="msgHandler"></web-view>
index.js頁面
Page({ msgHandler: function (e) { //(h5像小程序傳遞參數) console.log(e.detail.data) //獲取到來自也頁面的數據 var info = (e.detail.data)[0] this.setData({ value: info.value }); this.setData({ title: info.title }); }, onShareAppMessage: function (options) { //轉發時執行 var that = this; console.log(this.data.webUrl); return { title: that.data.title, path: '/pages/index/index?value=' + that.data.value,//小程序像h5傳遞參數 success(e) { // shareAppMessage: ok, // shareTickets 數組,每一項是一個 shareTicket ,對應一個轉發對象 // 需要在頁面onLoad()事件中實現接口 wx.showShareMenu({ // 要求小程序返回分享目標信息 withShareTicket: true }); }, } }, onload:function(options){ var webUrl = ''; if (options.value) {//獲取轉發過來的參數 webUrl = "https://www.jiquer.com/vity/gdsen.xhtm?value=" + options.value; } else { webUrl = "https://www.jiquer.com/vity/gdsen.xhtm" } this.setData({ webUrl: webUrl }) } })
h5的js頁面(獲取並處理小程序傳遞過來的參數)
function getParamer() { var url = window.location.href.split("?")[1]; /*獲取url里"?"后面的值*/ if (url) { /*判斷是否是一個參數還是多個參數*/ url = url.split("=") return url[1]; /*返回想要的參數值*/ } else { return ''; } }
本文鏈接:https://blog.csdn.net/qq_37235231/article/details/82053062