<!doctype html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="content-type"> <title> js调用java </title> </head> <body> <p> <xmp id="show"> </xmp> </p> <p> <xmp id="init"> </xmp> </p> </body> <script> var u = navigator.userAgent; function connectWebViewJavascriptBridge(callback) { var u = navigator.userAgent; if (u.indexOf('Android') > -1) {//安卓手机 //Android使用 if(window.WebViewJavascriptBridge) { callback(WebViewJavascriptBridge) } else { document.addEventListener('WebViewJavascriptBridgeReady', function() { callback(WebViewJavascriptBridge) }, false ); } } else {//苹果手机 //iOS使用 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) } } connectWebViewJavascriptBridge(function(bridge) { if(navigator.userAgent.indexOf('Android') > -1){ bridge.init(function(message, responseCallback) { console.log('JS got a message', message); var data = { 'Javascript Responds': '测试中文!' }; if (responseCallback) { console.log('JS responding with', data); responseCallback(data); } }); } bridge.registerHandler('postUserInfoTojs', function(data, responseCallback) { document.getElementById("show").innerHTML = ("data from Java: = " + data); if (responseCallback) { var responseData = "Javascript Says Right back aka!"; responseCallback(responseData); } }); }) </script> </html>
js 和 app交互信息
最新推荐文章于 2023-05-23 12:24:29 发布