Opening _blank links inside inappbrowser

馋奶兔 提交于 2019-12-06 00:54:02

As InAppBrowser is designed, there can be only one instance of it, so your only choice is to open the external links on the system browser. You can listen for the loadstart event and if the url doesn't contain your domain then open it on the system browser, something like this:

var browserRef = window.cordova.InAppBrowser.open('http://www.yourdomain.com/support', '_blank');

browserRef.addEventListener('loadstart', function(event) {
    if((event.url).indexOf('www.yourdomain.com') === -1) {
        window.cordova.InAppBrowser.open(event.url, '_system');
    }
});

But I don't think that will avoid also loading the external site on the inAppBrowser window, so you will have to also inject javascript to return to the previous page on the inAppBrowser

browserRef.addEventListener('loadstop', function(event) {
    browserRef.executeScript({ code: 'window.history.back();' }, null);
});

The answer by jcesarmobile only partially works.

When you open the _system InAppBrowser, the reference to browserRef gets lost, along with the events mapped to 'loadstart' and 'loadstop'. This is a very bad behavior of InAppBrowser that comes from long ago. No idea if they intend to fix it. I have submitted an issue just in case.

In other words, this will only work for the first external link you open.

Further page loads will not behave the same anymore. The very inelegant solution that I found so far, was to re-instantiate the "_blank" browser after launching the "_system" browser. But for that, you must store the navigation history to be able to launch a new InAppBrowser with the previous URL.

The code below works for me, but sometimes the new InAppBrowser does not get displayed and I am investigating why.

libs: "ionic-native": "2.2.11" "typescript": "2.0.9"

import { Component } from '@angular/core';
import { InAppBrowser, Splashscreen } from 'ionic-native';
import { Platform } from 'ionic-angular';
import 'rxjs/Rx';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  browser: InAppBrowser;
  sysBrowser: InAppBrowser;
  urlHistory: string[] = [];
  url: string;

  constructor(public platform: Platform) {
    this.platform = platform;
    this.url = 'http://www.yourdomain.com/support';
  }

  ionViewDidLoad() {
    this.platform.ready().then(() => {
      this.browser = new InAppBrowser(this.url, '_blank', 'location=no,zoom=no,hidden=no');
      this.urlHistory.push(this.url);
      this.addEventsToBlankBrowser();
    });
  }

  addEventsToBlankBrowser() {
      this.browser.on('loadstart').subscribe(
        event => {
          if (event.url.indexOf('www.yourdomain.com') === -1) {
            let previousUrl = this.urlHistory.pop();
            this.browser.executeScript({ code: 'window.location.href = "' + previousUrl + '";' });
            this.sysBrowser = new InAppBrowser(event.url, '_system');
            this.url = previousUrl;
            this.urlHistory.push(previousUrl);
            this.browser = new InAppBrowser(previousUrl, '_blank', 'location=yes,zoom=no,hidden=no');
            this.browser.show();
            this.addEventsToBlankBrowser();
          } else {
            this.url = event.url;
            this.urlHistory.push(event.url);
          }
        }
      );
  }
}

As its mentioned in previous comments: InAppBrowser solution currently only support one InAppBrowser instance at a time. With "@ionic-native/core" version 4.12.2, typescript 2.6.2 with angular code an example implementation looks like:

import { NavController, Platform, LoadingController } from 'ionic-angular';
import { InAppBrowser } from "@ionic-native/in-app-browser";

export class HomePage {
  ref;

  constructor(public platform:Platform, private iab: InAppBrowser) {
    platform.ready().then(() => {
      this.initApp();
    })
  }

    openIAB(url, target, options){
    this.ref = undefined
    this.ref = this.iab.create(url, target, options)
    this.ref.on('loadstop').subscribe((event) => {
      console.log('loadstop')      
      this.ref.show();
    });

    this.ref.on('exit').subscribe((event) => {
      console.log(' exit called ')
      this.platform.exitApp();      
      });      
    }

    openSystemWindow(){ this.openIAB('http://www.google.com','_system', {location: 'no'}) }

    initApp(){
      this.openIAB('http://www.google.com','_blank',{location:'no', zoom:'no',
      clearcache: 'yes', hidden: 'yes', hardwareback:'no'})
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!