2013-07-30 31 views
2

使用InAppBrowser當他選擇關閉InAppBrowser時,我希望獲取用戶當前所在網站的URL。獲取InAppBrowser的URL

爲退出事件添加eventlistener似乎沒有返回該URL。

我首先想到的解決方法是訂閱loadstart事件,並從那裏獲取URL,因此始終使用「最新」URL。但是,當用戶按下後退按鈕時,這並不總是觸發。

任何想法如何解決這個問題?

使用PhoneGap的2.8.0

回答

1

使用3.1這是相當簡單的

你只是負載開始添加事件。或加載停止。

var ref = window.open('http://apache.org', '_blank', 'location=yes'); 
ref.addEventListener('loadstart', function() { alert(event.url); }); 

看到科爾多瓦文檔的更多信息Cordova docs

+0

這是否工作方面,當用戶使用後退按鈕來? – Publicus

+0

是的,使用後退按鈕很好用 – Leo

+1

科爾多瓦4.x在這裏,看起來像'event.url'現在是不確定的:/ ...任何其他方式獲取網址? – Michael

1

獲取瀏覽器InAppBrowser在angularjs的URL,離子

你可以得到打開的窗口視圖(瀏覽器)的網址使用瀏覽器的事件處理(InAppBrowser)

作爲一個例子,我們正在調用一個twitter頁面,並且每當頁面c焊割的網址,它會顯示警告的URL:

var twitterWindow = window.open("https://twitter.com/", '_blank', 'location=no'); 
        twitterWindow.addEventListener('loadstart', function(event) { 
         alert("Current Event : "+ event.url); 
        }); 

這裏我們使用「的addEventListener」處理瀏覽器的事件。

window.open('local-url.html');     
// loads in the Cordova WebView 
window.open('local-url.html', '_self');   
// loads in the Cordova webView 
window.open('local-url.html', '_system');  
// Security error: system browser, but url will not load (iOS) 
window.open('local-url.html', '_blank');   
// loads in the InAppBrowser 
window.open('local-url.html', 'random_string'); 
// loads in the InAppBrowser 
window.open('local-url.html', 'random_string', 'location=no'); 
// loads in the InAppBrowser, no location bar 

要處理瀏覽器的事件,我們可以使用:

var ref = window.open('http://whitelisted-url.com', '_blank'); 
ref.addEventListener('loadstart', function(event) { alert(event.type + ' - ' + event.url); }); 
ref.addEventListener('loadstop', function(event) { alert(event.type + ' - ' + event.url); }); 
ref.addEventListener('loaderror', function(event) { alert(event.type + ' - ' + event.url + ' - ' + event.code + ' - ' + event.message); }); 
ref.addEventListener('exit', function(event) { alert(event.type); }); 

,並關閉InAppBrowser:

var ref = window.open('http://whitelisted-url.com', '_blank'); 
ref.close(); 
0

只是爲了完成獅子座的回答是:如果一切正常通event對象作爲loadstart函數的參數。

ref.addEventListener('loadstart', function(event) { alert(event.url); }); 
0

InAppBrowser退出不返回URL的事件。 爲了捕獲URL,您需要聽取loadstart事件。 這裏是爲我工作的例子:

//Open InAppBrowser on selected URL 
 
browser = this.inAppBrowser.create(initialURL, '_blank', 'location=yes'); 
 

 

 
//WARNING: Events will only fire in the app, not if running in a browser 
 
//Every time a page is opened in the InAppBrowser, store the URL 
 
if (browser.on('loadstart').subscribe) 
 
\t browser.on('loadstart').subscribe((e: InAppBrowserEvent) => { 
 
\t \t if (e && e.url) 
 
\t \t \t url = e.url; 
 
\t }); 
 

 
//When the InAppBrowser is closed, check and use the last viewed URL 
 
if (browser.on('exit').subscribe) 
 
\t browser.on('exit').subscribe((e: InAppBrowserEvent) => { 
 
\t \t if (url) 
 
\t \t \t ...use the last viewed URL...; 
 
\t }); 
 

 
\t \t \t