UNPKG

30.3 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
46If you change the browsers `window.open` function this way, it can have unintended side
47effects (especially if this plugin is included only as a dependency of another
48plugin).
49
50The InAppBrowser window behaves like a standard web browser,
51and can't access Cordova APIs. For this reason, the InAppBrowser is recommended
52if you need to load third-party (untrusted) content, instead of loading that
53into the main Cordova webview. The InAppBrowser is not subject to the
54whitelist, nor is opening links in the system browser.
55
56The InAppBrowser provides by default its own GUI controls for the user (back,
57forward, done).
58
59## Installation
60
61 cordova plugin add cordova-plugin-inappbrowser
62
63If you want all page loads in your app to go through the InAppBrowser, you can
64simply hook `window.open` during initialization. For example:
65
66 document.addEventListener("deviceready", onDeviceReady, false);
67 function onDeviceReady() {
68 window.open = cordova.InAppBrowser.open;
69 }
70
71### Preferences
72
73#### <b>config.xml</b>
74- <b>InAppBrowserStatusBarStyle [iOS only]</b>: (string, options 'lightcontent' or 'default'. Defaults to 'default') set text color style for iOS.
75```
76<preference name="InAppBrowserStatusBarStyle" value="lightcontent" />
77```
78
79## cordova.InAppBrowser.open
80
81Opens a URL in a new `InAppBrowser` instance, the current browser
82instance, or the system browser.
83
84 var ref = cordova.InAppBrowser.open(url, target, options);
85
86- __ref__: Reference to the `InAppBrowser` window when the target is set to `'_blank'`. _(InAppBrowser)_
87
88- __url__: The URL to load _(String)_. Call `encodeURI()` on this if the URL contains Unicode characters.
89
90- __target__: The target in which to load the URL, an optional parameter that defaults to `_self`. _(String)_
91
92 - `_self`: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the `InAppBrowser`.
93 - `_blank`: Opens in the `InAppBrowser`.
94 - `_system`: Opens in the system's web browser.
95
96- __options__: Options for the `InAppBrowser`. Optional, defaulting to: `location=yes`. _(String)_
97
98 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.
99
100 All platforms support:
101
102 - __location__: Set to `yes` or `no` to turn the `InAppBrowser`'s location bar on or off.
103
104 Android supports these additional options:
105
106 - __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.
107 - __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).
108 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
109 - __clearsessioncache__: set to `yes` to have the session cookie cache cleared before the new window is opened
110 - __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.
111 - __closebuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the
112 close button color from default, regardless of being a text or default X. Only has effect if user has location set to `yes`.
113 - __footer__: set to `yes` to show a close button in the footer similar to the iOS __Done__ button.
114 The close button will appear the same as for the header hence use __closebuttoncaption__ and __closebuttoncolor__ to set its properties.
115 - __footercolor__: set to a valid hex color string, for example `#00ff00` or `#CC00ff00` (`#aarrggbb`) , and it will change the footer color from default.
116 Only has effect if user has __footer__ set to `yes`.
117 - __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.
118 - __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`.
119 - __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`.
120 - __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`.
121 - __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`.
122 - __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the right and close button to the left. Default value is `no`.
123 - __zoom__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them. Default value is `yes`.
124 - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
125 - __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)).
126 - __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`).
127 - __fullscreen__: Sets whether the InappBrowser WebView is displayed fullscreen or not. In fullscreen mode, the status bar is hidden. Default value is `yes`.
128
129 iOS supports these additional options:
130
131 - __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.
132 - __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).
133 - __clearcache__: set to `yes` to have the browser's cookie cache cleared before the new window is opened
134 - __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.
135 - __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
136 - __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.
137 - __closebuttoncaption__: set to a string to use as the __Done__ button's caption. Note that you need to localize this value yourself.
138 - __disallowoverscroll__: Set to `yes` or `no` (default is `no`). Turns on/off the the bounce of the WKWebView's UIScrollView.
139 - __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.
140 - __navigationbuttoncolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color. Only applicable if navigation buttons are visible.
141 - __toolbar__: set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`)
142 - __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.
143 - __toolbartranslucent__: set to `yes` or `no` to make the toolbar translucent(semi-transparent) (defaults to `yes`). Only applicable if toolbar is not disabled.
144 - __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.
145 - __enableViewportScale__: Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`).
146 - __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
147 - __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`).
148 - __presentationstyle__: Set to `pagesheet`, `formsheet` or `fullscreen` to set the [presentation style](https://developer.apple.com/documentation/uikit/uimodalpresentationstyle) (defaults to `fullscreen`).
149 - __transitionstyle__: Set to `fliphorizontal`, `crossdissolve` or `coververtical` to set the [transition style](https://developer.apple.com/documentation/uikit/uimodaltransitionstyle) (defaults to `coververtical`).
150 - __toolbarposition__: Set to `top` or `bottom` (default is `bottom`). Causes the toolbar to be at the top or bottom of the window.
151 - __hidespinner__: Set to `yes` or `no` to change the visibility of the loading indicator (defaults to `no`).
152
153 Windows supports these additional options:
154
155 - __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.
156 - __hardwareback__: works the same way as on Android platform.
157 - __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.
158
159
160### Supported Platforms
161
162- Android
163- Browser
164- iOS
165- OSX
166- Windows
167
168### Example
169
170 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
171 var ref2 = cordova.InAppBrowser.open(encodeURI('http://ja.m.wikipedia.org/wiki/ハングル'), '_blank', 'location=yes');
172
173### OSX Quirks
174
175At the moment the only supported target in OSX is `_system`.
176
177`_blank` and `_self` targets are not yet implemented and are ignored silently. Pull requests and patches to get these to work are greatly appreciated.
178
179### iOS Quirks
180
181Since the introduction of iPadOS 13, iPads try to adapt their content mode / user agent for the optimal browsing experience. This may result in iPads having their user agent set to Macintosh, making it hard to detect them as mobile devices using user agent string sniffing. You can change this with the `PreferredContentMode` preference in `config.xml`.
182
183```xml
184<preference name="PreferredContentMode" value="mobile" />
185```
186
187The example above forces the user agent to contain `iPad`. The other option is to use the value `desktop` to turn the user agent to `Macintosh`.
188
189### Browser Quirks
190
191- Plugin is implemented via iframe,
192
193- Navigation history (`back` and `forward` buttons in LocationBar) is not implemented.
194
195## InAppBrowser
196
197The object returned from a call to `cordova.InAppBrowser.open` when the target is set to `'_blank'`.
198
199### Methods
200
201- addEventListener
202- removeEventListener
203- close
204- show
205- hide
206- executeScript
207- insertCSS
208
209## InAppBrowser.addEventListener
210
211> Adds a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`)
212
213 ref.addEventListener(eventname, callback);
214
215- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
216
217- __eventname__: the event to listen for _(String)_
218
219 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
220 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
221 - __loaderror__: event fires when the `InAppBrowser` encounters an error when loading a URL.
222 - __exit__: event fires when the `InAppBrowser` window is closed.
223 - __beforeload__: event fires when the `InAppBrowser` decides whether to load an URL or not (only with option `beforeload` set).
224 - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
225
226- __callback__: the function that executes when the event fires. The function is passed an `InAppBrowserEvent` object as a parameter.
227
228## Example
229
230```javascript
231
232var inAppBrowserRef;
233
234function showHelp(url) {
235
236 var target = "_blank";
237
238 var options = "location=yes,hidden=yes,beforeload=yes";
239
240 inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
241
242 inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);
243
244 inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);
245
246 inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);
247
248 inAppBrowserRef.addEventListener('beforeload', beforeloadCallBack);
249
250 inAppBrowserRef.addEventListener('message', messageCallBack);
251}
252
253function loadStartCallBack() {
254
255 $('#status-message').text("loading please wait ...");
256
257}
258
259function loadStopCallBack() {
260
261 if (inAppBrowserRef != undefined) {
262
263 inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;}" });
264
265 inAppBrowserRef.executeScript({ code: "\
266 var message = 'this is the message';\
267 var messageObj = {my_message: message};\
268 var stringifiedMessageObj = JSON.stringify(messageObj);\
269 webkit.messageHandlers.cordova_iab.postMessage(stringifiedMessageObj);"
270 });
271
272 $('#status-message').text("");
273
274 inAppBrowserRef.show();
275 }
276
277}
278
279function loadErrorCallBack(params) {
280
281 $('#status-message').text("");
282
283 var scriptErrorMesssage =
284 "alert('Sorry we cannot open that page. Message from the server is : "
285 + params.message + "');"
286
287 inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
288
289 inAppBrowserRef.close();
290
291 inAppBrowserRef = undefined;
292
293}
294
295function executeScriptCallBack(params) {
296
297 if (params[0] == null) {
298
299 $('#status-message').text(
300 "Sorry we couldn't open that page. Message from the server is : '"
301 + params.message + "'");
302 }
303
304}
305
306function beforeloadCallBack(params, callback) {
307
308 if (params.url.startsWith("http://www.example.com/")) {
309
310 // Load this URL in the inAppBrowser.
311 callback(params.url);
312 } else {
313
314 // The callback is not invoked, so the page will not be loaded.
315 $('#status-message').text("This browser only opens pages on http://www.example.com/");
316 }
317
318}
319
320function messageCallBack(params){
321 $('#status-message').text("message received: "+params.data.my_message);
322}
323
324```
325
326### InAppBrowserEvent Properties
327
328- __type__: the eventname, either `loadstart`, `loadstop`, `loaderror`, `message` or `exit`. _(String)_
329
330- __url__: the URL that was loaded. _(String)_
331
332- __code__: the error code, only in the case of `loaderror`. _(Number)_
333
334- __message__: the error message, only in the case of `loaderror`. _(String)_
335
336- __data__: the message contents , only in the case of `message`. A stringified JSON object. _(String)_
337
338
339### Supported Platforms
340
341- Android
342- Browser
343- iOS
344- Windows
345- OSX
346
347### Browser Quirks
348
349`loadstart`, `loaderror`, `message` events are not fired.
350
351### Windows Quirks
352
353`message` event is not fired.
354
355### Quick Example
356
357 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
358 ref.addEventListener('loadstart', function(event) { alert(event.url); });
359
360## InAppBrowser.removeEventListener
361
362> Removes a listener for an event from the `InAppBrowser`. (Only available when the target is set to `'_blank'`)
363
364 ref.removeEventListener(eventname, callback);
365
366- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
367
368- __eventname__: the event to stop listening for. _(String)_
369
370 - __loadstart__: event fires when the `InAppBrowser` starts to load a URL.
371 - __loadstop__: event fires when the `InAppBrowser` finishes loading a URL.
372 - __loaderror__: event fires when the `InAppBrowser` encounters an error loading a URL.
373 - __exit__: event fires when the `InAppBrowser` window is closed.
374 - __message__: event fires when the `InAppBrowser` receives a message posted from the page loaded inside the `InAppBrowser` Webview.
375
376- __callback__: the function to execute when the event fires.
377The function is passed an `InAppBrowserEvent` object.
378
379### Supported Platforms
380
381- Android
382- Browser
383- iOS
384- Windows
385
386### Quick Example
387
388 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
389 var myCallback = function(event) { alert(event.url); }
390 ref.addEventListener('loadstart', myCallback);
391 ref.removeEventListener('loadstart', myCallback);
392
393## InAppBrowser.close
394
395> Closes the `InAppBrowser` window.
396
397 ref.close();
398
399- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
400
401### Supported Platforms
402
403- Android
404- Browser
405- iOS
406- Windows
407
408### Quick Example
409
410 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
411 ref.close();
412
413## InAppBrowser.show
414
415> Displays an InAppBrowser window that was opened hidden. Calling this has no effect if the InAppBrowser was already visible.
416
417 ref.show();
418
419- __ref__: reference to the InAppBrowser window (`InAppBrowser`)
420
421### Supported Platforms
422
423- Android
424- Browser
425- iOS
426- Windows
427
428### Quick Example
429
430 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'hidden=yes');
431 // some time later...
432 ref.show();
433
434## InAppBrowser.hide
435
436> Hides the InAppBrowser window. Calling this has no effect if the InAppBrowser was already hidden.
437
438 ref.hide();
439
440- __ref__: reference to the InAppBrowser window (`InAppBrowser`)
441
442### Supported Platforms
443
444- Android
445- iOS
446- Windows
447
448### Quick Example
449
450 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank');
451 // some time later...
452 ref.hide();
453
454## InAppBrowser.executeScript
455
456> Injects JavaScript code into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`)
457
458 ref.executeScript(details, callback);
459
460- __ref__: reference to the `InAppBrowser` window. _(InAppBrowser)_
461
462- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
463 - __file__: URL of the script to inject.
464 - __code__: Text of the script to inject.
465
466- __callback__: the function that executes after the JavaScript code is injected.
467 - If the injected script is of type `code`, the callback executes
468 with a single parameter, which is the return value of the
469 script, wrapped in an `Array`. For multi-line scripts, this is
470 the return value of the last statement, or the last expression
471 evaluated.
472
473### Supported Platforms
474
475- Android
476- Browser
477- iOS
478- Windows
479
480### Quick Example
481
482 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
483 ref.addEventListener('loadstop', function() {
484 ref.executeScript({file: "myscript.js"});
485 });
486
487### Browser Quirks
488
489- only __code__ key is supported.
490
491### Windows Quirks
492
493Due 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]`.
494
495## InAppBrowser.insertCSS
496
497> Injects CSS into the `InAppBrowser` window. (Only available when the target is set to `'_blank'`)
498
499 ref.insertCSS(details, callback);
500
501- __ref__: reference to the `InAppBrowser` window _(InAppBrowser)_
502
503- __injectDetails__: details of the script to run, specifying either a `file` or `code` key. _(Object)_
504 - __file__: URL of the stylesheet to inject.
505 - __code__: Text of the stylesheet to inject.
506
507- __callback__: the function that executes after the CSS is injected.
508
509### Supported Platforms
510
511- Android
512- iOS
513- Windows
514
515### Quick Example
516
517 var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
518 ref.addEventListener('loadstop', function() {
519 ref.insertCSS({file: "mystyles.css"});
520 });
521__
522
523## <a id="sample"></a>Sample: Show help pages with an InAppBrowser
524
525You 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.
526
527Here's a few snippets that show how you do this.
528
529* [Give users a way to ask for help](#give).
530* [Load a help page](#load).
531* [Let users know that you're getting their page ready](#let).
532* [Show the help page](#show).
533* [Handle page errors](#handle).
534
535### <a id="give"></a>Give users a way to ask for help
536
537There's lots of ways to do this in your app. A drop down list is a simple way to do that.
538
539```html
540
541<select id="help-select">
542 <option value="default">Need help?</option>
543 <option value="article">Show me a helpful article</option>
544 <option value="video">Show me a helpful video</option>
545 <option value="search">Search for other topics</option>
546</select>
547
548```
549
550Gather 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.
551
552```javascript
553
554$('#help-select').on('change', function (e) {
555
556 var url;
557
558 switch (this.value) {
559
560 case "article":
561 url = "https://cordova.apache.org/docs/en/latest/"
562 + "reference/cordova-plugin-inappbrowser/index.html";
563 break;
564
565 case "video":
566 url = "https://youtu.be/F-GlVrTaeH0";
567 break;
568
569 case "search":
570 url = "https://www.google.com/#q=inAppBrowser+plugin";
571 break;
572 }
573
574 showHelp(url);
575
576});
577
578```
579
580### <a id="load"></a>Load a help page
581
582We'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.
583
584```javascript
585
586function showHelp(url) {
587
588 var target = "_blank";
589
590 var options = "location=yes,hidden=yes";
591
592 inAppBrowserRef = cordova.InAppBrowser.open(url, target, options);
593
594 inAppBrowserRef.addEventListener('loadstart', loadStartCallBack);
595
596 inAppBrowserRef.addEventListener('loadstop', loadStopCallBack);
597
598 inAppBrowserRef.addEventListener('loaderror', loadErrorCallBack);
599
600}
601
602```
603
604### <a id="let"></a>Let users know that you're getting their page ready
605
606Because 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.
607
608```javascript
609
610function loadStartCallBack() {
611
612 $('#status-message').text("loading please wait ...");
613
614}
615
616```
617
618### <a id="show"></a>Show the help page
619
620When 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.
621
622```javascript
623
624function loadStopCallBack() {
625
626 if (inAppBrowserRef != undefined) {
627
628 inAppBrowserRef.insertCSS({ code: "body{font-size: 25px;}" });
629
630 $('#status-message').text("");
631
632 inAppBrowserRef.show();
633 }
634
635}
636
637```
638You 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.
639
640### <a id="handle"></a>Handle page errors
641
642Sometimes 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.
643
644We'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.
645
646```javascript
647
648function loadErrorCallBack(params) {
649
650 $('#status-message').text("");
651
652 var scriptErrorMesssage =
653 "alert('Sorry we cannot open that page. Message from the server is : "
654 + params.message + "');"
655
656 inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
657
658 inAppBrowserRef.close();
659
660 inAppBrowserRef = undefined;
661
662}
663
664function executeScriptCallBack(params) {
665
666 if (params[0] == null) {
667
668 $('#status-message').text(
669 "Sorry we couldn't open that page. Message from the server is : '"
670 + params.message + "'");
671 }
672
673}
674
675```
676
677## More Usage Info
678
679### Local Urls ( source is in the app package )
680```
681var iab = cordova.InAppBrowser;
682
683iab.open('local-url.html'); // loads in the Cordova WebView
684iab.open('local-url.html', '_self'); // loads in the Cordova WebView
685iab.open('local-url.html', '_system'); // Security error: system browser, but url will not load (iOS)
686iab.open('local-url.html', '_blank'); // loads in the InAppBrowser
687iab.open('local-url.html', 'random_string'); // loads in the InAppBrowser
688iab.open('local-url.html', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
689
690```
691
692
693
694### Whitelisted Content
695
696```
697var iab = cordova.InAppBrowser;
698
699iab.open('http://whitelisted-url.com'); // loads in the Cordova WebView
700iab.open('http://whitelisted-url.com', '_self'); // loads in the Cordova WebView
701iab.open('http://whitelisted-url.com', '_system'); // loads in the system browser
702iab.open('http://whitelisted-url.com', '_blank'); // loads in the InAppBrowser
703iab.open('http://whitelisted-url.com', 'random_string'); // loads in the InAppBrowser
704
705iab.open('http://whitelisted-url.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
706
707```
708
709### Urls that are not white-listed
710
711```
712var iab = cordova.InAppBrowser;
713
714iab.open('http://url-that-fails-whitelist.com'); // loads in the InAppBrowser
715iab.open('http://url-that-fails-whitelist.com', '_self'); // loads in the InAppBrowser
716iab.open('http://url-that-fails-whitelist.com', '_system'); // loads in the system browser
717iab.open('http://url-that-fails-whitelist.com', '_blank'); // loads in the InAppBrowser
718iab.open('http://url-that-fails-whitelist.com', 'random_string'); // loads in the InAppBrowser
719iab.open('http://url-that-fails-whitelist.com', 'random_string', 'location=no'); // loads in the InAppBrowser, no location bar
720
721```