<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原生与H5交互</title>
<script>
window.function(){
var btn = document.getElementsByTagName('button')[0];
btn.onclick=function(){
alert(navigator.userAgent)
//H5调取原生方法,建立连接,并向原生传参
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
//做一下约定,ios那边去检测一下
//window.getPic();
try {
window.webkit.messageHandlers.getMessage.postMessage(jsonStr)
} catch(error) {
console.log(error)
}
}else{
window.my.getPic();
}
}
//原生调取H5方法,安卓和ios都可以用这一种方法
function showPic (str) {
//str则为原生传过来的数据
}
}
function getMessage(json){
//空方法
}
</script>
</head>
<body>
<button >跳转到原生页面</button>
</body>
</html>
h5与安卓和ios交互
最新推荐文章于 2024-05-15 09:28:30 发布