2013-02-27 2 views
0

로컬 HTML 파일에서 외부 URL을 열려고합니다. 예를 들어, super.loadUrl ("file : ///android_asset/www/index.html"); 로컬 파일을 호출하면 DroidGap에서 제공하는 브라우저에서 해당 파일을 엽니 다. 그러나 외부 URL에 문제가 있습니다. index.html에서 이미지 버튼을 통해 http://www.google.com에 연락하려고 시도하면 기기가 다른 브라우저를 열어 www.google.com을 표시합니다 (내 기기에서는 www.google.com 페이지가 열리기 시작합니다). DroidGap 브라우저가 DroidGap 브라우저에서 외부 URL과 로컬 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

사실 위의 2.3.0에 폰갭을 업그레이드하거나 경우, 자녀 브라우저 플러그인을 사용할 필요가 있도록 없습니다 :

따라서 사용자가 원격 페이지에서 멀리 탐색 할 때 브라우저를 닫

당신이 사용할 수있는 InAppBrowser

나는 t을 필요로했기 때문에 내가 이것을 달성

var ref = window.open(authorize_url, '_blank', 'location=no'); 
    ref.addEventListener('loadstart', function() { me.locChanged(event.url,ref); }); 
1

샘플 그는 같은 해결책입니다. 나는 = window.open

//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에 다음과 같이 추가하고 이벤트 리스너 ... 이미 로컬 파일하지만 단지의 경우를로드하는 방법을 알고 있다고 가정().

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

그런 다음/그냥 그렇게 광산/www /에서 페이지에 있는지 어떻게 당신의/www /에서 폴더에 어떤 곳 closeInAppBrowser.html라는 로컬 파일을 만들

는 지금에 Loadstart 함수를 정의 있도록 할 때()는,()로 window.open을 사용하여 이와 같은 링크와 JS를 배치하려고하는 SomeLocalFile.html에서 지금

//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(); 
} 

을 발광하는 페이지에 Loadstart를로드하기 시작합니다.

// 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'; 
}); 

이제 링크를 클릭 할 때 이것이에 Loadstart()을 트리거 closeInAppBrowser.html로 이동을 시도하고 event.url 파일 이름이 "closeInAppBrowser.html"를 일치하는 경우는 확인하고 만약 그렇다면 그것은 것 창을 닫습니다.

나는 이것을 테스트했으며 100 % 작동합니다. 다른 질문이 있으면 알려주세요

관련 문제