// 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) { } }