2013-03-20 66 views
18

我的PhoneGap Android應用程序的系統瀏覽器中未打開外部URL。我正在使用PhoneGap Build 2.3.0。PhoneGap Build:如何在Android設備瀏覽器中打開外部網址?

按照Cordova documentation我使用的目標 '_SYSTEM':

window.open('http://www.myurl.nl', '_system'); 

在我的config.xml中我有:

<plugin name="InAppBrowser" value="org.apache.cordova.InAppBrowser" /> 
<access origin="*" browserOnly="true" /> 

但還是鏈接我的應用程序的WebView打開。

如何解決這個問題?

+0

在我看來,它在Android中默認工作正常(但我使用Crosswalk插件也是...)。查看我的2015年答案,在WebView中打開'_self',在外部瀏覽器中打開'_blank':http://stackoverflow.com/questions/32208609/cordova-why-would-inappbrowser-plugin-be-required-to-open - 系統連接眉毛/ 32227524 – 2015-08-26 13:43:00

回答

15

,當你想使用PhoneGap的建設,以保持這不是問題的答案,但我解決了我的機器上設置的科爾多瓦(PhoneGap的)開發環境和編譯問題在本地應用。在科爾多瓦2.5.0 window.open('http://www.myurl.nl', '_system');工程完美,它會打開系統的瀏覽器中的鏈接。

所以我的建議是停止使用PhoneGap的建立,並開始在本地編譯您的應用程序。 Here's how to set up your development environment for Cordova >>

+0

你可以exaplain什麼可能是從本地編譯和phonegap構建的區別? – realtebo 2013-08-01 12:47:04

+1

差別主要在於設置。在本地編譯時,更改某些設置會更容易。但是因爲PhoneGap Build上的Cordova 2.5與本地安裝的結果並不相同,核心代碼中必然存在一些差異,但不知道爲什麼。 – Joan 2013-08-02 07:55:32

+0

@Joan真的我不確定這是一個phonegap構建問題。你確定使用了適當的插件聲明,並且插件可以通過Phonegap有效地加載到你的應用程序中嗎?我已經看到人們使用'gap:plugin'而不是'plugin'標籤,值得嘗試 – 2015-08-26 13:42:01

-1

使用此

window.open('http://www.myurl.nl', '_blank', 'location=yes');

+13

不,不起作用。 – Joan 2013-03-21 06:49:58

+0

爲什麼你會評論這個評論? 如果你檢查phonegap網站,這個命令行是InAppBrowser安裝指南的ac/p ... https://github.com/apache/cordova-plugin-inappbrowser/blob/dev/doc/index.md 你的配置文件也應該被拒絕 – Dimitri 2014-04-11 09:57:11

+0

你應該提到你的解決方案需要使用InAppBrowser插件。否則它不會。 – 2014-04-30 06:11:28

7

這個問題是現在有點老了,但我覺得這是值得更新。與2.9.0一起使用時,PhoneGap Build現在可以正常工作。

我已經在Android 4.3和iOS 6.1.3上編譯並測試了它。我沒有InAppBrowser插件在我的應用程序因爲這是在應用程序中打開網頁,而不是導致本地瀏覽器打開它們,我只有在訪問標籤以下內容:

<access origin="http://127.0.0.1*"/> 
<access origin="http://phonegap.com" subdomains="true" /> 
+0

在我的情況下工作。謝謝:) – 2014-06-10 07:11:09

+0

與phonegap 3.0,android cli build – 2014-06-10 07:20:46

14

遲到的回答,但可能是它可以幫助某人。

navigator.app.loadUrl('https://google.com/', { openExternal:true }); 

科爾多瓦3.3.1

+0

這對我來說是相當多的bug,我的應用程序將加載多個選項卡,即使鏈接只被點擊一次! – 2014-02-27 11:55:27

+0

@IanJamieson哦!不知何故它現在爲我工作 – AtanuCSE 2014-02-28 03:36:48

+0

我也是,我最終使用Hammer JS來收聽輕敲事件,這阻止了多次打開鏈接。 – 2014-02-28 09:51:51

6

這爲我工作。 Phonegap 3.1.0。

的html代碼:

<a id="ext-link" href="#">Google it</a> 

<button id="ext-link" href="#">Google it</button> 

的JavaScript(用jQuery +科爾多瓦):

$("#ext-link").on("click"), function() { 
    if (typeof navigator !== "undefined" && navigator.app) { 
     // Mobile device. 
     navigator.app.loadUrl('http://www.google.com/', {openExternal: true}); 
    } else { 
     // Possible web browser 
     window.open("http://www.google.com/", "_blank"); 
    } 
}); 

希望有所幫助。

+1

沒有幫我 04-11 11:47:48.186:E/Web控制檯(22063):Uncaught TypeError:無法調用方法'loadUrl'undefined:88 – Dimitri 2014-04-11 09:49:42

1

@George Siggouroglou:這不是用於元素的ID,最終將一個文檔中出現一個以上的時間是個好主意。取而代之的是讓代碼更加模塊化的好做法。

如果希望觸摸設備的也是不錯的選擇之前使用「點擊」,「點擊」,因爲它觸發速度更快,早於一點擊。檢查觸摸能力的東西,我喜歡使用modernizr,因爲它使功能檢測輕而易舉。

The jQuery Mobile tap event triggers after a quick, complete touch event that occurs on a single target object. It is the gesture equivalent of a standard click event that is triggered on the release state of the touch gesture. https://api.jquerymobile.com/tap/

希望幫助別人

**html code:** 

<a class="ext-link" href="#">Google it</a> 

<button class="ext-link" href="#">Google it</button> 

的JavaScript(用jQuery):

//define tab or click event type on rool level (can be combined with modernizr) 
iaEvent = "click"; 
if (typeof navigator !== "undefined" && navigator.app) { 
    iaEvent = "tap"; 
} 
$('.ext-link').each.bind(iaEvent, function() { 
    if (typeof navigator !== "undefined" && navigator.app) { 
     // Mobile device. 
     var linktarget = this.attr("href"); 
     navigator.app.loadUrl(linktarget, {openExternal: true}); 
    } else { 
     // Possible web browser 
     window.open(linktarget, "_blank"); 
    } 
}); 
相關問題