当前位置 : 主页 > 网络编程 > JavaScript >

JavaScriptBridge

来源:互联网 收集:自由互联 发布时间:2021-06-28
iOS function setupWebViewJavascriptBridge (callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callb
iOS
function setupWebViewJavascriptBridge (callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}

// 调用方法也很简单(这里仅举例js调用原生的方法close,并传参数{"type":1})
setupWebViewJavascriptBridge(function(bridge){
    bridge.callHandler("close", {"type":1});
})
Android
function connectWebViewJavascriptBridge (callback) {
    if (window.WebViewJavascriptBridge) {
        callback(WebViewJavascriptBridge)
    } else {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function() {
                callback(WebViewJavascriptBridge)
            },
            false
        );
    }
}

// 调用
connectWebViewJavascriptBridge(function(bridge){
    bridge.callHandler("close", {"type":1});
})
网友评论