// Swift webview.stringByEvaluatingJavaScriptFromString("Math.random()") // OC [webView stringByEvaluatingJavaScriptFromString:@"Math.random();"];
//下面为伪代码 webview.setDataToJs(somedata); webview.setDataToJs = function(data) { webview.stringByEvaluatingJavaScriptFromString("JSBridge.trigger(event, data)") }
JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; NSString *textJS = @"showAlert('这里是JS中alert弹出的message')"; [context evaluateScript:textJS];
jsbridge://methodName?param1=value1¶m2=value2
var url = 'jsbridge://doAction?title=分享标题&desc=分享描述&link=http%3A%2F%2Fwww.baidu.com'; var iframe = document.createElement('iframe'); iframe.style.width = '1px'; iframe.style.height = '1px'; iframe.style.display = 'none'; iframe.src = url; document.body.appendChild(iframe); setTimeout(function() { iframe.remove(); }, 100);
function secondClick() { share('分享的标题','分享的内容','图片地址'); }
<button type="button" onclick="secondClick()">Click Me!</button>
class JSInterface { @JavascriptInterface //注意这个代码必定要加上 public String getUserData() { return "UserData"; } } webView.addJavascriptInterface(new JSInterface(), "AndroidJS");
alert(AndroidJS.getUserData()) //UserDate
class YouzanWebChromeClient extends WebChromeClient { @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { // 这里就能够对js的prompt进行处理,经过result返回结果 } @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { } @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { } }