UNPKG

15.4 kBMarkdownView Raw
1<!--
2# license: Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18-->
19
20# cordova-plugin-inappbrowser
21
22This plugin provides a web browser view that displays when calling `cordova.InAppBrowser.open()`.
23
24 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
25
26The `cordova.InAppBrowser.open()` function is defined to be a drop-in replacement
27for the `window.open()` function. Existing `window.open()` calls can use the
28InAppBrowser window, by replacing window.open:
29
30 window.open = cordova.InAppBrowser.open;
31
32The InAppBrowser window behaves like a standard web browser,
33and can't access Cordova APIs. For this reason, the InAppBrowser is recommended
34if you need to load third-party (untrusted) content, instead of loading that
35into the main Cordova webview. The InAppBrowser is not subject to the
36whitelist, nor is opening links in the system browser.
37
38The InAppBrowser provides by default its own GUI controls for the user (back,
39forward, done).
40
41For backwards compatibility, this plugin also hooks `window.open`.
42However, the plugin-installed hook of `window.open` can have unintended side
43effects (especially if this plugin is included only as a dependency of another
44plugin). The hook of `window.open` will be removed in a future major release.
45Until the hook is removed from the plugin, apps can manually restore the default
46behaviour:
47
48 delete window.open // Reverts the call back to it's prototype's default
49
50Although `window.open` is in the global scope, InAppBrowser is not available until after the `deviceready` event.
51
52 document.addEventListener("deviceready", onDeviceReady, false);
53 function onDeviceReady() {
54 console.log("window.open works well");
55 }
56
57:warning: Report issues on the [Apache Cordova issue tracker](https://issues.apache.org/jira/issues/?jql=project%20%3D%20CB%20AND%20status%20in%20%28Open%2C%20%22In%20Progress%22%2C%20Reopened%29%20AND%20resolution%20%3D%20Unresolved%20AND%20component%20%3D%20%22Plugin%20InAppBrowser%22%20ORDER%20BY%20priority%20DESC%2C%20summary%20ASC%2C%20updatedDate%20DESC)
58
59
60
61## Installation
62
63 cordova plugin add cordova-plugin-inappbrowser
64
65If you want all page loads in your app to go through the InAppBrowser, you can
66simply hook `window.open` during initialization. For example:
67
68 document.addEventListener("deviceready", onDeviceReady, false);
69 function onDeviceReady() {
70 window.open = cordova.InAppBrowser.open;
71 }
72
73## cordova.InAppBrowser.open
74
75Opens a URL in a new `InAppBrowser` instance, the current browser
76instance, or the system browser.
77
78 var ref = cordova.InAppBrowser.open(url, target, options);
79
80- __ref__: Reference to the `InAppBrowser` window. _(InAppBrowser)_
81
82- __url__: The URL to load _(String)_. Call `encodeURI()` on this if the URL contains Unicode characters.
83
84- __target__: The target in which to load the URL, an optional parameter that defaults to `_self`. _(String)_
85
86 - `_self`: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the `InAppBrowser`.
87 - `_blank`: Opens in the `InAppBrowser`.
88 - `_system`: Opens in the system's web browser.
89
90- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `location=yes`. _(String)_
91
92 The `options` string must not contain any blank space, and each feature's name/value pairs must be separated by a comma. Feature names are case insensitive. All platforms support the value below:
93
94 - __location__: Set to `yes` or `no` to turn the `InAppBrowser`'s location bar on or off.
95
96 Android only:
97
98 - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
99 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
100 - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
101 - __zoom__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them. Default value is `yes`.
102 - __hardwareback__: set to `yes` to use the hardware back button to navigate backwards through the `InAppBrowser`'s history. If there is no previous page, the `InAppBrowser` will close. The default value is `yes`, so you must set it to `no` if you want the back button to simply close the InAppBrowser.
103
104 iOS only:
105
106 - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself.
107 - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the UIWebViewBounce property.
108 - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
109 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
110 - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
111 - __toolbar__: set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`)
112 - __enableViewportScale__: Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`).
113 - __mediaPlaybackRequiresUserAction__: Set to `yes` or `no` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
114 - __allowInlineMediaPlayback__: Set to `yes` or `no` to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. The HTML's `video` element must also include the `webkit-playsinline` attribute (defaults to `no`)
115 - __keyboardDisplayRequiresUserAction__: Set to `yes` or `no` to open the keyboard when form elements receive focus via JavaScript's `focus()` call (defaults to `yes`).
116 - __suppressesIncrementalRendering__: Set to `yes` or `no` to wait until all new view content is received before being rendered (defaults to `no`).
117 - __presentationstyle__: Set to `pagesheet`, `formsheet` or `fullscreen` to set the [presentation style](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/modalPresentationStyle) (defaults to `fullscreen`).
118 - __transitionstyle__: Set to `fliphorizontal`, `crossdissolve` or `coververtical` to set the [transition style](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/modalTransitionStyle) (defaults to `coververtical`).
119 - __toolbarposition__: Set to `top` or `bottom` (default is `bottom`). Causes the toolbar to be at the top or bottom of the window.
120
121 Windows only:
122
123 - __hidden__: set to `yes` to create the browser and load the page, but not show it. The loadstop event fires when loading is complete. Omit or set to `no` (default) to have the browser open and load normally.
124 - __fullscreen__: set to `yes` to create the browser control without a border around it. Please note that if __location=no__ is also specified, there will be no control presented to user to close IAB window.
125
126### Supported Platforms
127
128- Amazon Fire OS
129- Android
130- BlackBerry 10
131- Firefox OS
132- iOS
133- Windows 8 and 8.1
134- Windows Phone 7 and 8
135- Browser
136
137### Example
138
139 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
140 var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', 'location=yes');
141
142### Firefox OS Quirks
143
144As plugin doesn't enforce any design there is a need to add some CSS rules if
145opened with `target='_blank'`. The rules might look like these
146
147``` css
148.inAppBrowserWrap {
149 background-color: rgba(0,0,0,0.75);
150 color: rgba(235,235,235,1.0);
151}
152.inAppBrowserWrap menu {
153 overflow: auto;
154 list-style-type: none;
155 padding-left: 0;
156}
157.inAppBrowserWrap menu li {
158 font-size: 25px;
159 height: 25px;
160 float: left;
161 margin: 0 10px;
162 padding: 3px 10px;
163 text-decoration: none;
164 color: #ccc;
165 display: block;
166 background: rgba(30,30,30,0.50);
167}
168.inAppBrowserWrap menu li.disabled {
169 color: #777;
170}
171```
172
173### Windows Quirks
174
175Windows 8.0, 8.1 and Windows Phone 8.1 don't support remote urls to be opened in the Cordova WebView so remote urls are always showed in the system's web browser if opened with `target='_self'`.
176
177On Windows 10 if the URL is NOT in the white list and is opened with `target='_self'` it will be showed in the system's web browser instead of InAppBrowser popup.
178
179Similar to Firefox OS IAB window visual behaviour can be overridden via `inAppBrowserWrap`/`inAppBrowserWrapFullscreen` CSS classes
180
181### Browser Quirks
182
183- Plugin is implemented via iframe,
184
185- Navigation history (`back` and `forward` buttons in LocationBar) is not implemented.
186
187## InAppBrowser
188
189The object returned from a call to `cordova.InAppBrowser.open`.
190
191### Methods
192
193- addEventListener
194- removeEventListener
195- close
196- show
197- executeScript
198- insertCSS
199
200## addEventListener
201
202> Adds a listener for an event from the `InAppBrowser`.
203
204 ref.addEventListener(eventname, callback);
205
206- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
207
208- __eventname__: the event to listen for _(String)_
209
210 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
211 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
212 - __loaderror__: event fires when the `InAppBrowser` encounters an error when loading a URL.
213 - __exit__: event fires when the `InAppBrowser` window is closed.
214
215- __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
216
217### InAppBrowserEvent Properties
218
219- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, or `exit`. _(String)_
220
221- __url__: the URL that was loaded. _(String)_
222
223- __code__: the error code, only in the case of `loaderror`. _(Number)_
224
225- __message__: the error message, only in the case of `loaderror`. _(String)_
226
227
228### Supported Platforms
229
230- Amazon Fire OS
231- Android
232- iOS
233- Windows 8 and 8.1
234- Windows Phone 7 and 8
235- Browser
236
237### Browser Quirks
238
239`loadstart` and `loaderror` events are not being fired.
240
241### Quick Example
242
243 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
244 ref.addEventListener('loadstart', function(event) { alert(event.url); });
245
246## removeEventListener
247
248> Removes a listener for an event from the `InAppBrowser`.
249
250 ref.removeEventListener(eventname, callback);
251
252- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
253
254- __eventname__: the event to stop listening for. _(String)_
255
256 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
257 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
258 - __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL.
259 - __exit__: event fires when the `InAppBrowser` window is closed.
260
261- __callback__: the function to execute when the event fires.
262The function is passed an `InAppBrowserEvent` object.
263
264### Supported Platforms
265
266- Amazon Fire OS
267- Android
268- iOS
269- Windows 8 and 8.1
270- Windows Phone 7 and 8
271- Browser
272
273### Quick Example
274
275 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
276 var myCallback = function(event) { alert(event.url); }
277 ref.addEventListener('loadstart', myCallback);
278 ref.removeEventListener('loadstart', myCallback);
279
280## close
281
282> Closes the `InAppBrowser` window.
283
284 ref.close();
285
286- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
287
288### Supported Platforms
289
290- Amazon Fire OS
291- Android
292- Firefox OS
293- iOS
294- Windows 8 and 8.1
295- Windows Phone 7 and 8
296- Browser
297
298### Quick Example
299
300 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
301 ref.close();
302
303## show
304
305> Displays an InAppBrowser window that was opened hidden. Calling this has no effect if the InAppBrowser was already visible.
306
307 ref.show();
308
309- __ref__: reference to the InAppBrowser window (`InAppBrowser`)
310
311### Supported Platforms
312
313- Amazon Fire OS
314- Android
315- iOS
316- Windows 8 and 8.1
317- Browser
318
319### Quick Example
320
321 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'hidden=yes');
322 // some time later...
323 ref.show();
324
325## executeScript
326
327> Injects JavaScript code into the `InAppBrowser` window
328
329 ref.executeScript(details, callback);
330
331- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
332
333- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
334 - __file__: URL of the script to inject.
335 - __code__: Text of the script to inject.
336
337- __callback__: the function that executes after the JavaScript code is injected.
338 - If the injected script is of type `code`, the callback executes
339 with a single parameter, which is the return value of the
340 script, wrapped in an `Array`. For multi-line scripts, this is
341 the return value of the last statement, or the last expression
342 evaluated.
343
344### Supported Platforms
345
346- Amazon Fire OS
347- Android
348- iOS
349- Windows 8 and 8.1
350- Browser
351
352### Quick Example
353
354 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
355 ref.addEventListener('loadstop', function() {
356 ref.executeScript({file: "myscript.js"});
357 });
358
359### Browser Quirks
360
361- only __code__ key is supported.
362
363### Windows Quirks
364
365Due to [MSDN docs](https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx) the invoked script can return only string values, otherwise the parameter, passed to __callback__ will be `[null]`.
366
367## insertCSS
368
369> Injects CSS into the `InAppBrowser` window.
370
371 ref.insertCSS(details, callback);
372
373- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
374
375- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
376 - __file__: URL of the stylesheet to inject.
377 - __code__: Text of the stylesheet to inject.
378
379- __callback__: the function that executes after the CSS is injected.
380
381### Supported Platforms
382
383- Amazon Fire OS
384- Android
385- iOS
386- Windows
387
388### Quick Example
389
390 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
391 ref.addEventListener('loadstop', function() {
392 ref.insertCSS({file: "mystyles.css"});
393 });