| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <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>
- <p>
- <input type="text" id="text1" value="用户名(username)"/>
- </p>
- <p>
- <input type="text" id="text2" value="password"/>
- </p>
- <p>
- <input type="button" id="enter" value="发消息给Native" onclick="testClick();"
- />
- </p>
- <p>
- <input type="button" id="enter1" value="调用Native方法" onclick="testClick1();"
- />
- </p>
- <p>
- <input type="button" id="enter2" value="显示html" onclick="testDiv();"/>
- </p>
- <p>
- <input type="file" value="打开文件"/>
- </p>
- </body>
- <script>
- function testDiv() {
- document.getElementById("show").innerHTML = document.getElementsByTagName("html")[0].innerHTML;
- }
- function testClick() {
- var str1 = document.getElementById("text1").value;
- var str2 = document.getElementById("text2").value;
- //send message to native
- var data = {id: 1, content: "这是一个图片 <img src=\"a.png\"/> test\r\nhahaha"};
- WebViewJavascriptBridge.doSend(
- data
- , function(responseData) {
- document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData
- }
- );
- }
- function testClick1() {
- var str1 = document.getElementById("text1").value;
- var str2 = document.getElementById("text2").value;
- //call native method
- window.WebViewJavascriptBridge.callHandler(
- 'submitFromWeb'
- , {'param': '中文测试'}
- , function(responseData) {
- document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
- }
- );
- }
- function bridgeLog(logContent) {
- document.getElementById("show").innerHTML = logContent;
- }
- function connectWebViewJavascriptBridge(callback) {
- if (window.WebViewJavascriptBridge && WebViewJavascriptBridge.inited) {
- callback(WebViewJavascriptBridge)
- } else {
- document.addEventListener(
- 'WebViewJavascriptBridgeReady'
- , function() {
- callback(WebViewJavascriptBridge)
- },
- false
- );
- }
- }
- connectWebViewJavascriptBridge(function(bridge) {
- 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("functionInJs", function(data, responseCallback) {
- document.getElementById("show").innerHTML = ("data from Java: = " + data + Date.now());
- if (responseCallback) {
- var responseData = "Javascript Says Right back aka!";
- responseCallback(responseData);
- }
- });
- })
- </script>
- </html>
|