You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ma...@apache.org on 2026/04/29 18:03:33 UTC

(cordova-plugin-inappbrowser) 01/01: doc(readme): improve `addEventListener` examples

This is an automated email from the ASF dual-hosted git repository.

GitToTheHub pushed a commit to branch pr-doc-readme-improve-addEventListener-examples
in repository https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git

commit 694afa470e57393a5b96b6bf16a04e277eafa0cc
Author: Manuel Beck <ma...@outlook.de>
AuthorDate: Wed Apr 29 20:03:22 2026 +0200

    doc(readme): improve `addEventListener` examples
    
    - No extra methods for callbacks. Use arrow funtion instad.
    - Add `inAppBrowserEvent` parameter for all callbacks
    - Use let and const instead of var
    - Replace JQuery code `$('#status-message').text(...)` with plain JavaScript: `document.getElementById("status-message").textContent = `
---
 README.md | 130 ++++++++++++++++++++++++--------------------------------------
 1 file changed, 50 insertions(+), 80 deletions(-)

diff --git a/README.md b/README.md
index 0180ae9..bbd2871 100644
--- a/README.md
+++ b/README.md
@@ -221,102 +221,72 @@ The object returned from a call to `cordova.InAppBrowser.open` when the target i
 
 - __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
 
-## Example
+### Example
 
 ```javascript
 
-var inAppBrowserRef;
-
 function showHelp(url) {
 
-    var target = "_blank";
-
-    var options = "location=yes,hidden=yes,beforeload=yes";
-
-    inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
-
-    inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);
-
-    inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);
-
-    inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);
-
-    inAppBrowserRef.addEventListener('beforeload', beforeloadCallBack);
-
-    inAppBrowserRef.addEventListener('message', messageCallBack);
-}
-
-function loadStartCallBack() {
-
-    $('#status-message').text("loading please wait ...");
+    let inAppBrowserRef = cordova.InAppBrowser.open(url, "_blank", "location=yes,hidden=yes,beforeload=yes");
 
-}
-
-function loadStopCallBack() {
+    inAppBrowserRef.addEventListener('loadstart', (inAppBrowserEvent) => {
+        document.getElementById("status-message").textContent = "loading please wait ...";
+    });
 
-    if (inAppBrowserRef != undefined) {
+    inAppBrowserRef.addEventListener('loadstop', (inAppBrowserEvent) => {
+        if (inAppBrowserRef == undefined) return;
 
         inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;}" });
-
-        inAppBrowserRef.executeScript({ code: "\
-            var message = 'this is the message';\
-            var messageObj = {my_message: message};\
-            var stringifiedMessageObj = JSON.stringify(messageObj);\
-            webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
+        inAppBrowserRef.executeScript({
+            code:
+            `
+                const message = 'this is the message';
+                const messageObj = {my_message: message};
+                const stringifiedMessageObj = JSON.stringify(messageObj);
+                webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);
+            `
         });
 
-        $('#status-message').text("");
-
+        document.getElementById("status-message").textContent = "";
         inAppBrowserRef.show();
-    }
-
-}
-
-function loadErrorCallBack(params) {
-
-    $('#status-message').text("");
-
-    var scriptErrorMesssage =
-       "alert('Sorry we cannot open that page. Message from the server is : "
-       + params.message + "');"
-
-    inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
-
-    inAppBrowserRef.close();
-
-    inAppBrowserRef = undefined;
-
-}
-
-function executeScriptCallBack(params) {
-
-    if (params[0] == null) {
-
-        $('#status-message').text(
-           "Sorry we couldn't open that page. Message from the server is : '"
-           + params.message + "'");
-    }
-
-}
-
-function beforeloadCallBack(params, callback) {
-
-    if (params.url.startsWith("http://www.example.com/")) {
-
-        // Load this URL in the inAppBrowser.
-        callback(params.url);
-    } else {
+    });
 
-        // The callback is not invoked, so the page will not be loaded.
-        $('#status-message').text("This browser only opens pages on http://www.example.com/");
-    }
+    inAppBrowserRef.addEventListener('loaderror', (inAppBrowserEvent) => {
+        document.getElementById("status-message").textContent = "";
+
+        const scriptErrorMesssage = alert(
+            `Sorry we cannot open that page. Message from the server is : ${inAppBrowserEvent.message}`
+        );
+
+        inAppBrowserRef.executeScript(
+            { code: scriptErrorMesssage }, 
+            // callback
+            (params) => {
+                if (params[0] == null) {
+                    document.getElementById("status-message").textContent =
+                        `Sorry we couldn't open that page. Message from the server is : '${params.message}'`;
+                }
+            }
+        );
+
+        inAppBrowserRef.close();
+        inAppBrowserRef = undefined;
+    });
 
-}
+    inAppBrowserRef.addEventListener('beforeload', (inAppBrowserEvent, callback) => {
+        if (inAppBrowserEvent.url.startsWith("http://www.example.com/")) {
+            // Load this URL in the inAppBrowser.
+            callback(inAppBrowserEvent.url);
+        } else {
+            // The callback is not invoked, so the page will not be loaded.
+            document.getElementById("status-message").textContent = "This browser only opens pages on http://www.example.com/";
+        }
+    });
 
-function messageCallBack(params){
-    $('#status-message').text("message received: "+params.data.my_message);
+    inAppBrowserRef.addEventListener('message', (inAppBrowserEvent) => {
+       document.getElementById("status-message").textContent = `message received: ${inAppBrowserEvent.data.my_message}`;
+    });
 }
-
 ```
 
 ### Customscheme event Example


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org