11<!doctype html>
22< html > < head >
3- < style type ='text/css '> h1 { color : red; }</ style >
3+ < style type ='text/css '>
4+ h1 { color : red; }
5+ .logLine { border-bottom : 1px solid # ccc ; padding : 4px 2px ; font-family : courier; font-size : 11px ; }
6+ </ style >
47</ head > < body >
58 < h1 > Javascript Bridge Demo</ h1 >
69 < script >
710 document . addEventListener ( 'WebViewJavascriptBridgeReady' , onBridgeReady , false )
811 function onBridgeReady ( ) {
912
13+ var uniqueId = 1
1014 function log ( message , data ) {
1115 var log = document . getElementById ( 'log' )
1216 var el = document . createElement ( 'div' )
13- el . innerHTML = message + ': ' + JSON . stringify ( data )
17+ el . className = 'logLine'
18+ el . innerHTML = uniqueId ++ + '. ' + message + ( data ? ': ' + JSON . stringify ( data ) : '' )
1419 if ( log . children . length ) { log . insertBefore ( el , log . children [ 0 ] ) }
1520 else { log . appendChild ( el ) }
1621 }
@@ -19,23 +24,26 @@ <h1>Javascript Bridge Demo</h1>
1924 } )
2025
2126 WebViewJavascriptBridge . registerHandler ( 'testJavascriptHandler' , function ( data , responseCallback ) {
22- log ( 'testJavascriptHandler' , data )
23- responseCallback ( { 'foo ' :'Right back atcha!' } )
27+ log ( 'JS handler testJavascriptHandler was called ' , data )
28+ responseCallback ( { 'Javascript Says ' :'Right back atcha!' } )
2429 } )
2530
2631 var button = document . getElementById ( 'buttons' ) . appendChild ( document . createElement ( 'button' ) )
2732 button . innerHTML = 'Send message to ObjC'
28- button . ontouchstart = function ( ) {
33+ button . ontouchstart = function ( e ) {
34+ e . preventDefault ( )
2935 WebViewJavascriptBridge . send ( 'Hello from JS button' )
3036 }
3137
3238 document . body . appendChild ( document . createElement ( 'br' ) )
3339
3440 var callbackButton = document . getElementById ( 'buttons' ) . appendChild ( document . createElement ( 'button' ) )
3541 callbackButton . innerHTML = 'Fire testObjcCallback'
36- callbackButton . ontouchstart = function ( ) {
42+ callbackButton . ontouchstart = function ( e ) {
43+ e . preventDefault ( )
44+ log ( "Calling handler testObjcCallback" )
3745 WebViewJavascriptBridge . fireHandler ( 'testObjcCallback' , { 'foo' : 'bar' } , function ( response ) {
38- log ( 'Got response:' , response )
46+ log ( 'Got response from testObjcCallback :' , response )
3947 } )
4048 }
4149 }
0 commit comments