📜  Cordova-InAppBrowser(1)

📅  最后修改于: 2023-12-03 15:00:02.124000             🧑  作者: Mango

Cordova InAppBrowser

Cordova InAppBrowser is a plugin for Cordova applications that allows displaying web pages within the application in a separate browser window.

Introduction

In the Cordova framework, it is possible to use the window.open() function to open a new browser window and display a web page. However, this method has some limitations, such as not being able to access device-specific features like the camera or contacts.

The InAppBrowser plugin solves this problem by allowing web pages to be displayed in a separate browser window within the application that can still access device-specific features. This window is independent of the main Cordova web view and can be closed and opened at any time.

Installation

To use the Cordova InAppBrowser plugin, you need to install it first. You can install the plugin using the Cordova CLI:

cordova plugin add cordova-plugin-inappbrowser

Once the plugin is installed, you can use it in your application.

Usage

To use the InAppBrowser plugin, you need to call the window.open() function and provide a URL to the web page you want to display. The InAppBrowser plugin provides additional options that you can use to customize the behavior of the browser window.

var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');

The open() function takes three parameters:

  • url: the URL of the web page to display
  • target: the target window where the page should be loaded. It can be set to _self (default), _blank, _system, or a custom name.
  • options: a string containing a comma-separated list of options to customize the behavior of the window.

The options parameter can include any of the following values:

  • location: set to yes to show a location bar
  • toolbar: set to yes to show a toolbar
  • zoom: set to yes to allow zooming
  • hidden: set to yes to create the browser window hidden until loaded
  • clearcache: set to yes to clear the cache
  • clearsessioncache: set to yes to clear the session cookie cache
  • closebuttoncaption: set the title of the close button
  • hardwareback: set to yes to enable the hardware back button
  • mediaPlaybackRequiresUserAction: set to yes to disable autoplay of media elements
  • shouldPauseOnSuspend: set to yes to pause the InAppBrowser when the application is suspended
  • footer: set to yes to show a footer bar
  • footercolor: set the color of the footer bar
  • footerheight: set the height of the footer bar
  • footercustom: set a custom HTML code to show in the footer bar
  • keyboardDisplayRequiresUserAction: set to yes to show the keyboard for text input fields only after the user touches the field.
Events

The InAppBrowser plugin provides events that you can use to handle browser window events, such as when the window is loaded or when the user closes the window.

// Setup the event listeners
ref.addEventListener('loadstart', function(event) { alert('Loading started: ' + event.url); });
ref.addEventListener('loadstop', function(event) { alert('Loading finished: ' + event.url); });
ref.addEventListener('exit', function(event) { alert('Browser window closed'); });

The addEventListener() function takes two parameters:

  • type: the type of event to listen to. It can be loadstart, loadstop, or exit.
  • listener: a function that will be called when the event occurs.
Conclusion

The Cordova InAppBrowser plugin is a powerful tool that allows displaying web pages within a Cordova application in a separate browser window. It is easy to use and provides many customization options to fit the needs of any application. With the use of events, you can handle the behavior of the browser window and provide a great user experience.