JavaScript events

View as Markdown

This section shows you how to create a WKUserScript from a JavaScript context to:

  • Pass the current environment parameters to the web view.
  • Terminate the current session and process commands for callback functions.
  • Handle messages that the host app should process for handoff events.

Inject environment parameters into WebView

To pass the current environment parameters to the web view, create a WKUserScript.

Here is an example in iOS Swift.

Note: This example only works with Mobile SDK.

let userContentController = WKUserContentController()
let webviewConfig = WKWebViewConfiguration()
webviewConfig.userContentController = userContentController

let injectScriptStr = """
window.zoomCampaignSdkConfig = window.zoomCampaignSdkConfig || {

    language: 'user language',
    firstName: 'user first name',
    lastName: 'user last name',
    nickName: 'user nick name',
    address: 'user address',
    company: 'user company',
    email: 'user email',
    phoneNumber: 'user phone number'
};

"""
let injectScript = WKUserScript(source: injectScriptStr, injectionTime: .atDocumentStart, forMainFrameOnly: false)
userContentController.addUserScript(injectScript)

Handle exit callback functions

To handle the termination of the current session and process commands from the JavaScript context, create a WKUserScript.

Here is an example in iOS Swift.

let exitHandlerScriptStr = """
window.addEventListener('zoomCampaignSdk:ready', () => {
    if (window.zoomCampaignSdk) {
        window.zoomCampaignSdk.native = {
            exitHandler: {
                handle: function() {
                    window.webkit.messageHandlers.zoomLiveSDKMessageHandler.postMessage('close_web_vc');
                }
            },
            commonHandler: {
                handle: function(e) {
                    window.webkit.messageHandlers.commonMessageHandler.postMessage(JSON.stringify(e));
                }
            }
        }
    }
})
"""
let exitScript = WKUserScript(source: exitHandlerScriptStr, injectionTime: .atDocumentStart, forMainFrameOnly: false)
userContentController.addUserScript(exitScript)

Handle handoff events

To handle messages from the JavaScript context that the host app should process for handoff events, create a WKUserScript.

Here is an example in iOS Swift.

let supportHandoffScriptStr = """
window.addEventListener('support_handoff', (e) => {
    window.webkit.messageHandlers.support_handoff.postMessage(JSON.stringify(e.detail));
});

"""
let supportHandoffScript = WKUserScript(source: supportHandoffScriptStr, injectionTime: .atDocumentStart, forMainFrameOnly: false)
userContentController.addUserScript(supportHandoffScript)