Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Detect Webview URL Change

I'm trying to sort out what event to attach to to detect when a webview's url (location) changes.

The sequence of events would be:

  1. Open webview to http://domain-a.com

  2. User completes form, gets redirected to http://my-totally-different-domain.com

  3. Redirect triggers Titanium webview event which I capture.

— asked September 8th 2010 by Michael Buckbee
  • location
  • mobile
  • url
  • webview
0 Comments

2 Answers

  • The following code will fire whenever the url of the webview changes:

    webView.addEventListener('load',function(){
        Ti.API.debug('The URL changed to '+webView.url);
    });
    

    And this code will fire whenever the webview redirects to the page you specify, if you put this code within the event listener:

    var params = "";
    var parts = (webView.url).replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        params = params + m;
    });
    url_base = (webView.url).replace(params,'');
    
    if (url_base == "http://www.example.com") {
        Ti.API.debug('we have arrived at the page you're waiting for');
    }
    

    For other methods of firing events from webviews, see this post.

    — answered March 25th 2011 by Joe iEntry
    permalink
    2 Comments
    • right

      — commented March 25th 2011 by Emrah Mehmedov
    • Note that this regex will not handle query params missing equals signs such as:

      http://www.example.com?foo&bar
      

      For that you could try something like:

      replace(/[?&]+([^=&]+)(=([^&]*))?/gi, ...)
      

      — commented February 6th 2015 by Benjamin Drucker
  • Set height to webView

    Thanks

    — answered January 19th 2015 by Arun Shejul
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.