应用场景:点击评论a的时候,H5给安卓和ios传conmentid;
<!DOCTYPE
html
>
<
html
>
<
head
lang=
"en"
>
<
meta
charset=
"UTF-8"
>
<
title
></
title
>
<
script
src=
"js/jquery.js"
></
script
>
</
head
>
<
body
>
<
input
type=
"button"
value=
"评论a"
onclick=
"
comment
(
123456
)
"
style=
"
width
:
100
px
;
height
:
50
px
;
"
>
<
script
type=
"text/javascript"
>
//上面不用看固定写法
var
brt
=
navigator
.
userAgent
.
toLowerCase
();
if
(
/iphone|ipad|ipod/
.
test
(
brt
))
{
function
setupWebViewJavascriptBridge
(callback) {
if
(
window
.WebViewJavascriptBridge) {
return
callback(WebViewJavascriptBridge); }
if
(
window
.
WVJBCallbacks
) {
return
window
.
WVJBCallbacks
.
push
(callback); }
window
.
WVJBCallbacks
= [callback];
var
WVJBIframe
=
document
.
createElement
(
'iframe'
);
WVJBIframe
.
style
.
display
=
'none'
;
WVJBIframe
.
src
=
'wvjbscheme://__BRIDGE_LOADED__'
;
document
.
documentElement
.
appendChild
(
WVJBIframe
);
setTimeout
(
function
() {
document
.
documentElement
.
removeChild
(
WVJBIframe
) },
0
)
}
}
//点击事件触发comment方法,传进来一个参数commentid
function
comment
(commentid){
var
ua
=
navigator
.
userAgent
.
toLowerCase
();
//判断:如果是iphone
if
(
/iphone|ipad|ipod/
.
test
(
ua
))
{
setupWebViewJavascriptBridge
(
function
(bridge)
{
/*ios端,和ios定一个共同的方法comment,传参的key=id,value=123456*/
bridge.callHandler(
'comment'
, {
'id'
:commentid },
function
(response) {
})
});
}
/*安卓端:跟安卓端确定一个共同的类名YunYou,和一个方法名comment即可,直接把参数commentid放到()里,安卓端就能收到commentid了**/
else if
(
/android/
.
test
(
ua
))
{
//android方法
YunYou.comment(commentid);
}
};
</
script
>
</
body
>
</
html
>