<p> 内容 </p> <script> window.onerror = function(err) { log(‘window.onerror: ‘ + err) } function connectWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { callback(WebViewJavascriptBridge) } else { document.addEventListener(‘WebViewJavascriptBridgeReady‘, function() { callback(WebViewJavascriptBridge) }, false) } } connectWebViewJavascriptBridge(function(bridge) { var uniqueId = 1 function log(message, data) { // var log = document.getElementById(‘log‘) // var el = document.createElement(‘div‘) // el.className = ‘logLine‘ // el.innerHTML = uniqueId++ + ‘. ‘ + message + ‘:<br /> ‘ + JSON.stringify(data) // if (log.children.length) { log.insertBefore(el, log.children[0]) } // else { log.appendChild(el) } } bridge.init(function(message, responseCallback) { log(‘JS got a message‘, message) var data = { ‘Javascript Responds‘:‘Wee!‘ } log(‘JS responding with‘, data) responseCallback(data) }) bridge.registerHandler(‘testJavascriptHandler‘, function(data, responseCallback) { log(‘ObjC called testJavascriptHandler with‘, data) var responseData = { ‘Javascript Says‘:‘Right back atcha!‘ } log(‘JS responding with‘, responseData) responseCallback(responseData) }) //测试回调 var callbackButton = document.getElementById(‘buttons‘) callbackButton.onclick = function(e) { e.preventDefault() log(‘JS calling handler "testObjcCallback"‘) bridge.callHandler(‘testObjcCallback‘, {‘m_id‘: ‘2999‘}, function(response) { log(‘JS got response‘, response) }) } }) </script> <p> <br /> </p> <p> <br /> </p> <div id="buttons"> 价格:500元 <a href="#">查看详情</a> <div id="log"> </div> </div> <p> <br /> </p>
原文:http://www.cnblogs.com/lsl8966/p/4992810.html