Getting URL of InAppBrowser

前端 未结 4 778
故里飘歌
故里飘歌 2021-01-12 18:22

Using InAppBrowser I wish to get the URL of the site the user is currently on, when he chooses to close the InAppBrowser.

Adding an eventlistener for the exit event

相关标签:
4条回答
  • 2021-01-12 18:48

    Getting URL of browser InAppBrowser in angularjs, Ionic

    You can get the url of the Opened window view (browser) by using the event handling of browser (InAppBrowser)

    As a example we are calling a twitter page and whenever the page change the url it will show the alert with the Url :

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

    Here we use the "addEventListener" for handling the event of browser .

     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
    

    To handle the events of the Browser we can use :

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

    And to Close the InAppBrowser :

    var ref = window.open('http://whitelisted-url.com', '_blank');
    ref.close();
    
    0 讨论(0)
  • 2021-01-12 18:55

    Just to complete Leo's answer: everything works fine if pass event object as an argument to loadstart function.

    ref.addEventListener('loadstart', function(event) { alert(event.url); });
    
    0 讨论(0)
  • 2021-01-12 19:03

    Using 3.1 it is fairly simple

    you just add an event on load start. or load stop.

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

    see the cordova docs for more info Cordova docs

    0 讨论(0)
  • 2021-01-12 19:04

    InAppBrowser exits event which does not return an URL. In order to capture the URL, you need to listen to the loadstart events. Here is an example that works for me:

    //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)
    	browser.on('loadstart').subscribe((e: InAppBrowserEvent) => {
    		if (e && e.url)
    			url = e.url; 
    	});
    
    //When the InAppBrowser is closed, check and use the last viewed URL
    if (browser.on('exit').subscribe)
    	browser.on('exit').subscribe((e: InAppBrowserEvent) => {
    		if (url)
    			...use the last viewed URL...;
    	});
    
    			

    0 讨论(0)
提交回复
热议问题