UNPKG

31.1 kBMarkdownView Raw
1---
2title: Inappbrowser
3description: Open an in-app browser window.
4---
5<!--
6# license: Licensed to the Apache Software Foundation (ASF) under one
7# or more contributor license agreements. See the NOTICE file
8# distributed with this work for additional information
9# regarding copyright ownership. The ASF licenses this file
10# to you under the Apache License, Version 2.0 (the
11# "License"); you may not use this file except in compliance
12# with the License. You may obtain a copy of the License at
13#
14# http://www.apache.org/licenses/LICENSE-2.0
15#
16# Unless required by applicable law or agreed to in writing,
17# software distributed under the License is distributed on an
18# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19# KIND, either express or implied. See the License for the
20# specific language governing permissions and limitations
21# under the License.
22-->
23
24|AppVeyor|Travis CI|
25|:-:|:-:|
26|[![Build status](https://ci.appveyor.com/api/projects/status/github/apache/cordova-plugin-inappbrowser?branch=master)](https://ci.appveyor.com/project/ApacheSoftwareFoundation/cordova-plugin-inappbrowser)|[![Build Status](https://travis-ci.org/apache/cordova-plugin-inappbrowser.svg?branch=master)](https://travis-ci.org/apache/cordova-plugin-inappbrowser)|
27
28# cordova-plugin-inappbrowser
29
30You can show helpful articles, videos, and web resources inside of your app. Users can view web pages without leaving your app.
31
32> To get a few ideas, check out the [sample](#sample) at the bottom of this page or go straight to the [reference](#reference) content.
33
34This plugin provides a web browser view that displays when calling `cordova.InAppBrowser.open()`.
35
36 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
37
38### `window.open`
39
40The `cordova.InAppBrowser.open()` function is defined to be a drop-in replacement
41for the `window.open()` function. Existing `window.open()` calls can use the
42InAppBrowser window, by replacing window.open:
43
44 window.open = cordova.InAppBrowser.open;
45
46The InAppBrowser window behaves like a standard web browser,
47and can't access Cordova APIs. For this reason, the InAppBrowser is recommended
48if you need to load third-party (untrusted) content, instead of loading that
49into the main Cordova webview. The InAppBrowser is not subject to the
50whitelist, nor is opening links in the system browser.
51
52The InAppBrowser provides by default its own GUI controls for the user (back,
53forward, done).
54
55For backwards compatibility, this plugin also hooks `window.open`.
56However, the plugin-installed hook of `window.open` can have unintended side
57effects (especially if this plugin is included only as a dependency of another
58plugin). The hook of `window.open` will be removed in a future major release.
59Until the hook is removed from the plugin, apps can manually restore the default
60behaviour:
61
62 delete window.open // Reverts the call back to its prototype's default
63
64Although `window.open` is in the global scope, InAppBrowser is not available until after the `deviceready` event.
65
66 document.addEventListener("deviceready", onDeviceReady, false);
67 function onDeviceReady() {
68 console.log("window.open works well");
69 }
70
71## Installation
72
73 cordova plugin add cordova-plugin-inappbrowser
74
75If you want all page loads in your app to go through the InAppBrowser, you can
76simply hook `window.open` during initialization. For example:
77
78 document.addEventListener("deviceready", onDeviceReady, false);
79 function onDeviceReady() {
80 window.open = cordova.InAppBrowser.open;
81 }
82
83## cordova.InAppBrowser.open
84
85Opens a URL in a new `InAppBrowser` instance, the current browser
86instance, or the system browser.
87
88 var ref = cordova.InAppBrowser.open(url, target, options);
89
90- __ref__: Reference to the `InAppBrowser` window when the target is set to `'_blank'`. _(InAppBrowser)_
91
92- __url__: The URL to load _(String)_. Call `encodeURI()` on this if the URL contains Unicode characters.
93
94- __target__: The target in which to load the URL, an optional parameter that defaults to `_self`. _(String)_
95
96 - `_self`: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the `InAppBrowser`.
97 - `_blank`: Opens in the `InAppBrowser`.
98 - `_system`: Opens in the system's web browser.
99
100- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `location=yes`. _(String)_
101
102 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.
103
104 All platforms support:
105
106 - __location__: Set to `yes` or `no` to turn the `InAppBrowser`'s location bar on or off.
107
108 Android supports these additional options:
109
110 - __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.
111 - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error).
112 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
113 - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
114 - __closebuttoncaption__: set to a string to use as the close button's caption instead of a X. Note that you need to localize this value yourself.
115 - __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the
116 close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`.
117 - __footer__: set to `yes` to show a close button in the footer similar to the iOS __Done__ button.
118 The close button will appear the same as for the header hence use __closebuttoncaption__ and __closebuttoncolor__ to set its properties.
119 - __footercolor__: set to a valid hex color string, for example `#00ff00` or `#CC00ff00` (`#aarrggbb`) , and it will change the footer color from default.
120 Only has effect if user has __footer__ set to `yes`.
121 - __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.
122 - __hidenavigationbuttons__: set to `yes` to hide the navigation buttons on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
123 - __hideurlbar__: set to `yes` to hide the url bar on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
124 - __navigationbuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color of both navigation buttons from default. Only has effect if user has location set to `yes` and not hidenavigationbuttons set to `yes`.
125 - __toolbarcolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color the toolbar from default. Only has effect if user has location set to `yes`.
126 - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the left and close button to the right.
127 - __zoom__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them. Default value is `yes`.
128 - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
129 - __shouldPauseOnSuspend__: Set to `yes` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)).
130 - __useWideViewPort__: Sets whether the WebView should enable support for the "viewport" HTML meta tag or should use a wide viewport. When the value of the setting is `no`, the layout width is always set to the width of the WebView control in device-independent (CSS) pixels. When the value is `yes` and the page contains the viewport meta tag, the value of the width specified in the tag is used. If the page does not contain the tag or does not provide a width, then a wide viewport will be used. (defaults to `yes`).
131
132 iOS supports these additional options:
133
134 - __usewkwebview__: set to `yes` to use WKWebView engine for the InappBrowser. Omit or set to `no` (default) to use UIWebView. Note: Using `usewkwebview=yes` requires that a WKWebView engine plugin be installed in the Cordova project (e.g. [cordova-plugin-wkwebview-engine](https://github.com/apache/cordova-plugin-wkwebview-engine) or [cordova-plugin-ionic-webview](https://github.com/ionic-team/cordova-plugin-ionic-webview)).
135 - __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.
136 - __beforeload__: set to enable the `beforeload` event to modify which pages are actually loaded in the browser. Accepted values are `get` to intercept only GET requests, `post` to intercept on POST requests or `yes` to intercept both GET & POST requests. Note that POST requests are not currently supported and will be ignored (if you set `beforeload=post` it will raise an error).
137 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
138 - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened. For WKWebView, requires iOS 11+ on target device.
139 - __cleardata__: set to `yes` to have the browser's entire local storage cleared (cookies, HTML5 local storage, IndexedDB, etc.) before the new window is opened
140 - __closebuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default __Done__ button's color. Only applicable if toolbar is not disabled.
141 - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself.
142 - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the UIWebViewBounce property.
143 - __hidenavigationbuttons__: set to `yes` or `no` to turn the toolbar navigation buttons on or off (defaults to `no`). Only applicable if toolbar is not disabled.
144 - __navigationbuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color. Only applicable if navigation buttons are visible.
145 - __toolbar__: set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`)
146 - __toolbarcolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color of the toolbar. Only applicable if toolbar is not disabled.
147 - __toolbartranslucent__: set to `yes` or `no` to make the toolbar translucent(semi-transparent) (defaults to `yes`). Only applicable if toolbar is not disabled.
148 - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, close button goes to the right and navigation buttons to the left.
149 - __enableViewportScale__: Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`). Only applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`) on iOS 10+.
150 - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`). Applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`).
151 - __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`). Applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`).
152 - __keyboardDisplayRequiresUserAction__: Set to `yes` or `no` to open the keyboard when form elements receive focus via JavaScript's `focus()` call (defaults to `yes`). Only applicable to UIWebView (`usewkwebview=no`).
153 - __suppressesIncrementalRendering__: Set to `yes` or `no` to wait until all new view content is received before being rendered (defaults to `no`). Only applicable to UIWebView (`usewkwebview=no`).
154 - __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`).
155 - __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`).
156 - __toolbarposition__: Set to `top` or `bottom` (default is `bottom`). Causes the toolbar to be at the top or bottom of the window.
157 - __hidespinner__: Set to `yes` or `no` to change the visibility of the loading indicator (defaults to `no`).
158
159 Windows supports these additional options:
160
161 - __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.
162 - __hardwareback__: works the same way as on Android platform.
163 - __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.
164
165
166### Supported Platforms
167
168- Android
169- Browser
170- iOS
171- OSX
172- Windows
173
174### Example
175
176 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
177 var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', 'location=yes');
178
179### OSX Quirks
180
181At the moment the only supported target in OSX is `_system`.
182
183`_blank` and `_self` targets are not yet implemented and are ignored silently. Pull requests and patches to get these to work are greatly appreciated.
184
185### Browser Quirks
186
187- Plugin is implemented via iframe,
188
189- Navigation history (`back` and `forward` buttons in LocationBar) is not implemented.
190
191## InAppBrowser
192
193The object returned from a call to `cordova.InAppBrowser.open` when the target is set to `'_blank'`.
194
195### Methods
196
197- addEventListener
198- removeEventListener
199- close
200- show
201- hide
202- executeScript
203- insertCSS
204
205## InAppBrowser.addEventListener
206
207> Adds a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`)
208
209 ref.addEventListener(eventname, callback);
210
211- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
212
213- __eventname__: the event to listen for _(String)_
214
215 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
216 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
217 - __loaderror__: event fires when the `InAppBrowser` encounters an error when loading a URL.
218 - __exit__: event fires when the `InAppBrowser` window is closed.
219 - __beforeload__: event fires when the `InAppBrowser` decides whether to load an URL or not (only with option `beforeload` set).
220 - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
221
222- __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
223
224## Example
225
226```javascript
227
228var inAppBrowserRef;
229
230function showHelp(url) {
231
232 var target = "_blank";
233
234 var options = "location=yes,hidden=yes,beforeload=yes";
235
236 inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
237
238 inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);
239
240 inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);
241
242 inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);
243
244 inAppBrowserRef.addEventListener('beforeload', beforeloadCallBack);
245
246 inAppBrowserRef.addEventListener('message', messageCallBack);
247}
248
249function loadStartCallBack() {
250
251 $('#status-message').text("loading please wait ...");
252
253}
254
255function loadStopCallBack() {
256
257 if (inAppBrowserRef != undefined) {
258
259 inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;" });
260
261 inAppBrowserRef.executeScript({ code: "\
262 var message = 'this is the message';\
263 var messageObj = {my_message: message};\
264 var stringifiedMessageObj = JSON.stringify(messageObj);\
265 webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
266 });
267
268 $('#status-message').text("");
269
270 inAppBrowserRef.show();
271 }
272
273}
274
275function loadErrorCallBack(params) {
276
277 $('#status-message').text("");
278
279 var scriptErrorMesssage =
280 "alert('Sorry we cannot open that page. Message from the server is : "
281 + params.message + "');"
282
283 inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
284
285 inAppBrowserRef.close();
286
287 inAppBrowserRef = undefined;
288
289}
290
291function executeScriptCallBack(params) {
292
293 if (params[0] == null) {
294
295 $('#status-message').text(
296 "Sorry we couldn't open that page. Message from the server is : '"
297 + params.message + "'");
298 }
299
300}
301
302function beforeloadCallBack(params, callback) {
303
304 if (params.url.startsWith("http://www.example.com/")) {
305
306 // Load this URL in the inAppBrowser.
307 callback(params.url);
308 } else {
309
310 // The callback is not invoked, so the page will not be loaded.
311 $('#status-message').text("This browser only opens pages on http://www.example.com/");
312 }
313
314}
315
316function messageCallBack(params){
317 $('#status-message').text("message received: "+params.data.my_message);
318}
319
320```
321
322### InAppBrowserEvent Properties
323
324- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, `message` or `exit`. _(String)_
325
326- __url__: the URL that was loaded. _(String)_
327
328- __code__: the error code, only in the case of `loaderror`. _(Number)_
329
330- __message__: the error message, only in the case of `loaderror`. _(String)_
331
332- __data__: the message contents , only in the case of `message`. A stringified JSON object. _(String)_
333
334
335### Supported Platforms
336
337- Android
338- Browser
339- iOS
340- Windows
341- OSX
342
343### Browser Quirks
344
345`loadstart`, `loaderror`, `message` events are not fired.
346
347### Windows Quirks
348
349`message` event is not fired.
350
351### Quick Example
352
353 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
354 ref.addEventListener('loadstart', function(event) { alert(event.url); });
355
356## InAppBrowser.removeEventListener
357
358> Removes a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`)
359
360 ref.removeEventListener(eventname, callback);
361
362- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
363
364- __eventname__: the event to stop listening for. _(String)_
365
366 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
367 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
368 - __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL.
369 - __exit__: event fires when the `InAppBrowser` window is closed.
370 - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
371
372- __callback__: the function to execute when the event fires.
373The function is passed an `InAppBrowserEvent` object.
374
375### Supported Platforms
376
377- Android
378- Browser
379- iOS
380- Windows
381
382### Quick Example
383
384 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
385 var myCallback = function(event) { alert(event.url); }
386 ref.addEventListener('loadstart', myCallback);
387 ref.removeEventListener('loadstart', myCallback);
388
389## InAppBrowser.close
390
391> Closes the `InAppBrowser` window.
392
393 ref.close();
394
395- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
396
397### Supported Platforms
398
399- Android
400- Browser
401- iOS
402- Windows
403
404### Quick Example
405
406 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
407 ref.close();
408
409## InAppBrowser.show
410
411> Displays an InAppBrowser window that was opened hidden. Calling this has no effect if the InAppBrowser was already visible.
412
413 ref.show();
414
415- __ref__: reference to the InAppBrowser window (`InAppBrowser`)
416
417### Supported Platforms
418
419- Android
420- Browser
421- iOS
422- Windows
423
424### Quick Example
425
426 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'hidden=yes');
427 // some time later...
428 ref.show();
429
430## InAppBrowser.hide
431
432> Hides the InAppBrowser window. Calling this has no effect if the InAppBrowser was already hidden.
433
434 ref.hide();
435
436- __ref__: reference to the InAppBrowser window (`InAppBrowser`)
437
438### Supported Platforms
439
440- Android
441- iOS
442- Windows
443
444### Quick Example
445
446 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank');
447 // some time later...
448 ref.hide();
449
450## InAppBrowser.executeScript
451
452> Injects JavaScript code into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`)
453
454 ref.executeScript(details, callback);
455
456- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
457
458- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
459 - __file__: URL of the script to inject.
460 - __code__: Text of the script to inject.
461
462- __callback__: the function that executes after the JavaScript code is injected.
463 - If the injected script is of type `code`, the callback executes
464 with a single parameter, which is the return value of the
465 script, wrapped in an `Array`. For multi-line scripts, this is
466 the return value of the last statement, or the last expression
467 evaluated.
468
469### Supported Platforms
470
471- Android
472- Browser
473- iOS
474- Windows
475
476### Quick Example
477
478 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
479 ref.addEventListener('loadstop', function() {
480 ref.executeScript({file: "myscript.js"});
481 });
482
483### Browser Quirks
484
485- only __code__ key is supported.
486
487### Windows Quirks
488
489Due 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]`.
490
491## InAppBrowser.insertCSS
492
493> Injects CSS into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`)
494
495 ref.insertCSS(details, callback);
496
497- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
498
499- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
500 - __file__: URL of the stylesheet to inject.
501 - __code__: Text of the stylesheet to inject.
502
503- __callback__: the function that executes after the CSS is injected.
504
505### Supported Platforms
506
507- Android
508- iOS
509- Windows
510
511### Quick Example
512
513 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
514 ref.addEventListener('loadstop', function() {
515 ref.insertCSS({file: "mystyles.css"});
516 });
517__
518
519## <a id="sample"></a>Sample: Show help pages with an InAppBrowser
520
521You can use this plugin to show helpful documentation pages within your app. Users can view online help documents and then close them without leaving the app.
522
523Here's a few snippets that show how you do this.
524
525* [Give users a way to ask for help](#give).
526* [Load a help page](#load).
527* [Let users know that you're getting their page ready](#let).
528* [Show the help page](#show).
529* [Handle page errors](#handle).
530
531### <a id="give"></a>Give users a way to ask for help
532
533There's lots of ways to do this in your app. A drop down list is a simple way to do that.
534
535```html
536
537<select id="help-select">
538 <option value="default">Need help?</option>
539 <option value="article">Show me a helpful article</option>
540 <option value="video">Show me a helpful video</option>
541 <option value="search">Search for other topics</option>
542</select>
543
544```
545
546Gather the users choice in the ``onDeviceReady`` function of the page and then send an appropriate URL to a helper function in some shared library file. Our helper function is named ``showHelp()`` and we'll write that function next.
547
548```javascript
549
550$('#help-select').on('change', function (e) {
551
552 var url;
553
554 switch (this.value) {
555
556 case "article":
557 url = "https://cordova.apache.org/docs/en/latest/"
558 + "reference/cordova-plugin-inappbrowser/index.html";
559 break;
560
561 case "video":
562 url = "https://youtu.be/F-GlVrTaeH0";
563 break;
564
565 case "search":
566 url = "https://www.google.com/#q=inAppBrowser+plugin";
567 break;
568 }
569
570 showHelp(url);
571
572});
573
574```
575
576### <a id="load"></a>Load a help page
577
578We'll use the ``open`` function to load the help page. We're setting the ``hidden`` property to ``yes`` so that we can show the browser only after the page content has loaded. That way, users don't see a blank browser while they wait for content to appear. When the ``loadstop`` event is raised, we'll know when the content has loaded. We'll handle that event shortly.
579
580```javascript
581
582function showHelp(url) {
583
584 var target = "_blank";
585
586 var options = "location=yes,hidden=yes";
587
588 inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
589
590 inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);
591
592 inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);
593
594 inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);
595
596}
597
598```
599
600### <a id="let"></a>Let users know that you're getting their page ready
601
602Because the browser doesn't immediately appear, we can use the ``loadstart`` event to show a status message, progress bar, or other indicator. This assures users that content is on the way.
603
604```javascript
605
606function loadStartCallBack() {
607
608 $('#status-message').text("loading please wait ...");
609
610}
611
612```
613
614### <a id="show"></a>Show the help page
615
616When the ``loadstopcallback`` event is raised, we know that the content has loaded and we can make the browser visible. This sort of trick can create the impression of better performance. The truth is that whether you show the browser before content loads or not, the load times are exactly the same.
617
618```javascript
619
620function loadStopCallBack() {
621
622 if (inAppBrowserRef != undefined) {
623
624 inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;" });
625
626 $('#status-message').text("");
627
628 inAppBrowserRef.show();
629 }
630
631}
632
633```
634You might have noticed the call to the ``insertCSS`` function. This serves no particular purpose in our scenario. But it gives you an idea of why you might use it. In this case, we're just making sure that the font size of your pages have a certain size. You can use this function to insert any CSS style elements. You can even point to a CSS file in your project.
635
636### <a id="handle"></a>Handle page errors
637
638Sometimes a page no longer exists, a script error occurs, or a user lacks permission to view the resource. How or if you handle that situation is completely up to you and your design. You can let the browser show that message or you can present it in another way.
639
640We'll try to show that error in a message box. We can do that by injecting a script that calls the ``alert`` function. That said, this won't work in browsers on Windows devices so we'll have to look at the parameter of the ``executeScript`` callback function to see if our attempt worked. If it didn't work out for us, we'll just show the error message in a ``<div>`` on the page.
641
642```javascript
643
644function loadErrorCallBack(params) {
645
646 $('#status-message').text("");
647
648 var scriptErrorMesssage =
649 "alert('Sorry we cannot open that page. Message from the server is : "
650 + params.message + "');"
651
652 inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
653
654 inAppBrowserRef.close();
655
656 inAppBrowserRef = undefined;
657
658}
659
660function executeScriptCallBack(params) {
661
662 if (params[0] == null) {
663
664 $('#status-message').text(
665 "Sorry we couldn't open that page. Message from the server is : '"
666 + params.message + "'");
667 }
668
669}
670
671```
672
673## More Usage Info
674
675### Local Urls ( source is in the app package )
676```
677var iab = cordova.InAppBrowser;
678
679iab.open('local-url.html'); // loads in the Cordova WebView
680iab.open('local-url.html', '_self'); // loads in the Cordova WebView
681iab.open('local-url.html', '_system'); // Security error: system browser, but url will not load (iOS)
682iab.open('local-url.html', '_blank'); // loads in the InAppBrowser
683iab.open('local-url.html', 'random_string'); // loads in the InAppBrowser
684iab.open('local-url.html', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
685
686```
687
688
689
690### Whitelisted Content
691
692```
693var iab = cordova.InAppBrowser;
694
695iab.open('http://whitelisted-url.com'); // loads in the Cordova WebView
696iab.open('http://whitelisted-url.com', '_self'); // loads in the Cordova WebView
697iab.open('http://whitelisted-url.com', '_system'); // loads in the system browser
698iab.open('http://whitelisted-url.com', '_blank'); // loads in the InAppBrowser
699iab.open('http://whitelisted-url.com', 'random_string'); // loads in the InAppBrowser
700
701iab.open('http://whitelisted-url.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
702
703```
704
705### Urls that are not white-listed
706
707```
708var iab = cordova.InAppBrowser;
709
710iab.open('http://url-that-fails-whitelist.com'); // loads in the InAppBrowser
711iab.open('http://url-that-fails-whitelist.com', '_self'); // loads in the InAppBrowser
712iab.open('http://url-that-fails-whitelist.com', '_system'); // loads in the system browser
713iab.open('http://url-that-fails-whitelist.com', '_blank'); // loads in the InAppBrowser
714iab.open('http://url-that-fails-whitelist.com', 'random_string'); // loads in the InAppBrowser
715iab.open('http://url-that-fails-whitelist.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
716
717```