2013-02-27 107 views
0

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

回答

0

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

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

cb.onLocationChange = function(loc){ 
    console.log("location = " + loc); 
    if (loc != "http://example.com") { 
     cb.close(); 
    } 
}; 
0

其实没有必要使用儿童浏览器插件,如果升级到的PhoneGap 2.3.0或以上,从而使您可以使用InAppBrowser

样品

var ref = window.open(authorize_url, '_blank', 'location=no'); 
    ref.addEventListener('loadstart', function() { me.locChanged(event.url,ref); }); 
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参考您的应用程序的主要JS = window.open ()。

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

然后创建一个名为closeInAppBrowser.html本地文件在/ WWW /文件夹一些地方恰好地雷是在/ WWW /页/

现在定义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(); 
} 
在SomeLocalFile.html

现在你打算使用window.open()的,安置一个链接和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%。如果您有任何其他问题,请告知我

相关问题