📅  最后修改于: 2023-12-03 15:00:02.124000             🧑  作者: Mango
Cordova InAppBrowser is a plugin for Cordova applications that allows displaying web pages within the application in a separate browser window.
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.
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.
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 displaytarget
: 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 bartoolbar
: set to yes
to show a toolbarzoom
: set to yes
to allow zoominghidden
: set to yes
to create the browser window hidden until loadedclearcache
: set to yes
to clear the cacheclearsessioncache
: set to yes
to clear the session cookie cacheclosebuttoncaption
: set the title of the close buttonhardwareback
: set to yes
to enable the hardware back buttonmediaPlaybackRequiresUserAction
: set to yes
to disable autoplay of media elementsshouldPauseOnSuspend
: set to yes
to pause the InAppBrowser when the application is suspendedfooter
: set to yes
to show a footer barfootercolor
: set the color of the footer barfooterheight
: set the height of the footer barfootercustom
: set a custom HTML code to show in the footer barkeyboardDisplayRequiresUserAction
: set to yes
to show the keyboard for text input fields only after the user touches the field.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.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.