ローカルの html ファイルから外部 URL を開こうとしています。たとえば、私は super.loadUrl("file:///android_asset/www/index.html"); を使用しています。ローカル ファイルを呼び出すと、アプリケーションは DroidGap が提供するブラウザーでそれを開きます。ただし、外部 URL に問題があります。index.html で、画像ボタンからhttp://www.google.comにアクセスしようとすると、デバイスが別のブラウザを開いて www.google.com を表示します (私のデバイスでは、chrome を開いてページ www. Google COM)。DroidGap ブラウザーで外部 URL とローカル URL の両方を開くことができるようにしたいと考えています。
3 に答える
同じソリューションが必要だったので、これを達成しました。ローカルファイルをロードする方法はすでに知っていると思いますが、念のため...
//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% 動作します。他にご不明な点がありましたらお知らせください
実際、phonegapを2.3.0以降にアップグレードして、 InAppBrowserを使用できるようにする場合は、子ブラウザープラグインを使用する必要はありません。
サンプル
var ref = window.open(authorize_url, '_blank', 'location=no');
ref.addEventListener('loadstart', function() { me.locChanged(event.url,ref); });
You want to use the ChildBrowser plugin to open the external web page. Then you want to set the ChildBrowser.onLocationChange
property to your own function. Then when the person navigates away from the remote page you will be notified of the location change so you can then close the ChildBrowser
and navigate to a new local page. You won't even need to touch the remote html page.
So to close the browser when the user navigates away from the remote page:
cb.onLocationChange = function(loc){
console.log("location = " + loc);
if (loc != "http://example.com") {
cb.close();
}
};