cordova phonegap: handling links from within inapp

2019-04-15 02:57发布

问题:

I made a phonegap/cordova application that just skins a mobile friendly website. I'm loading the website via the InAppBrowser plugin fullscreen (no address bar or 'next' buttons)

This works fine and dandy, but when you click on an external link from the website, it changes the location on the current browser and there is no way to return :(

I am trying to use the executeScript function to send a script to the InAppBrowser to catch links that I want to load in a new browser, but the window.open call just changes the url of the existing full screen one and doesn't open a new one.

Another option would be to open in the system browser, but that isnt working either.

Here is the code I am calling with app.initialize()

initialize: function() {
  document.addEventListener('deviceready', function () {
    //opening my initial window
    var ref = window.open('http://www.mywebsite.com', '_blank', 'location=no,toolbar=no');
    ref.addEventListener('loadstop', function() {
        //when the initial window finishing loading, execute this script
        ref.executeScript(
        {code: "$(document).on('click','a',function(e){
            var $this = $(this);
            if($this.hasClass('external-link')){
            //attempt to load a new inappbrowswer for .external-linl
            var newWindow = window.open($this.attr('href'), '_blank', 'location=yes,toolbar=yes')};
            e.preventDefault })"
        });
    });
  }, false);
}

Thanks for any guidance you can provide!