This is an automated email from the ASF dual-hosted git repository.
GitToTheHub pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/cordova-plugin-inappbrowser.git
The following commit(s) were added to refs/heads/master by this push:
new 7a90dde doc(readme): improve `addEventListener` examples (#1142)
7a90dde is described below
commit 7a90dde90503363b7352cc3e99c57d1dc8c91b13
Author: Manuel Beck <[email protected]>
AuthorDate: Tue May 5 18:41:25 2026 +0200
doc(readme): improve `addEventListener` examples (#1142)
- 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 2d5a65b..a19030d 100644
--- a/README.md
+++ b/README.md
@@ -222,102 +222,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: [email protected]
For additional commands, e-mail: [email protected]