0

我正在尝试从本地 html 文件打开外部 url。例如,我正在使用 super.loadUrl("file:///android_asset/www/index.html"); 调用本地文件,应用程序在 DroidGap 提供的浏览器中打开它。但是,外部网址存在问题。在 index.html 中,我试图通过图像按钮访问http://www.google.com,并且设备会打开另一个浏览器以显示 www.google.com(在我的设备中,chrome 正在打开以显示页面 www.google.com)。 google.com)。我想让 DroidGap 浏览器在 DroidGap 浏览器中打开外部和本地 url。

4

3 回答 3

1

我已经实现了这一点,因为我需要相同的解决方案。我假设您已经知道如何加载本地文件,但以防万一......

//By "SomeLocalFile.html" I mean an html file in the /www/ folder of the actual app installation **not** on the server
var ref = window.open('somelocalfile.html', '_blank', 'location=no');

首先将像这样的事件侦听器添加到您从中调用 var ref = window.open() 的应用程序的主 js 中。

ref.addEventListener('loadstart', LoadStart); // in YourMainJs.js

然后在 /www/ 文件夹中的某个位置创建一个名为 closeInAppBrowser.html 的本地文件,碰巧地雷位于 /www/pages/

现在定义 LoadStart 函数,以便在页面开始加载时 LoadStart() 将触发

//I use match(/[^/]+$/g) because I find that matching the file name is just easier than the path.
// This code is place in in YourMainJs.js
fileName = event.url.match(/[^/]+$/g);
if(fileName[0] == "closeInAppBrowser.html"){
    // alert("fun load stop runs");
    ref.close();
} 

现在在您将要使用 window.open() 的 SomeLocalFile.html 中,放置一个链接和 js,如下所示。

// in SomeLocalFile.html that was called via the window.open()
<a class="close">Close This Window Like A BOSS!</a>

$('.close').click(function(){
    //We use window location cause you can't navigate to any other local file just by using an href in an <a> tag
    //We aslo use ../../ because that is the correct path usage from within InAppBrowser to another local file
    window.location = '../../pages/closeInAppBrowser.html';   
});

现在,当您单击该链接时,它将尝试导航到 closeInAppBrowser.html,这将触发 LoadStart(),它将检查 event.url 文件名是否与“closeInAppBrowser.html”匹配,如果匹配,它将关闭窗口。

我已经对此进行了测试,它可以 100% 工作。如果您还有其他问题,请告诉我

于 2013-05-10T22:38:45.067 回答
0

您想使用ChildBrowser插件打开外部网页。然后你想将该ChildBrowser.onLocationChange属性设置为你自己的函数。然后,当此人离开远程页面时,您将收到位置更改通知,以便您可以关闭ChildBrowser并导航到新的本地页面。您甚至不需要触摸远程 html 页面。

因此,当用户离开远程页面时关闭浏览器:

cb.onLocationChange = function(loc){
    console.log("location = " + loc);
    if (loc != "http://example.com") {
        cb.close();
    }
}; 
于 2013-02-27T08:34:43.227 回答
0

其实不需要使用子浏览器插件,如果升级phonegap到2.3.0或以上,就可以使用InAppBrowser了

样本

  var ref = window.open(authorize_url, '_blank', 'location=no');
  ref.addEventListener('loadstart', function() {  me.locChanged(event.url,ref); });
于 2013-02-27T09:17:59.017 回答