Titanium Community Questions & Answer Archive

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

Getting A URL in a child Window

Quick sharing of something I figured out.

NOTE: This has only been used on Titanium Desktop. It may be totally different in Mobile.

Some Oauth authentication methods require you to run part of it through the web with a callback URL where they provide the key or token appended to the URL. You can pop open a window and send the user through the auth process, but how do you know when it's completed and what the returned code is?

I thought that the Titanium.UI.UserWindow.getURL method would work, but that always returns the initial value the window was opened with, not the value of the window's current URL.

Then I discovered Titanium.UI.UserWindow.getWindow. It basically makes a copy of the child window's window object in memory that you can inspect to your heart's delight.

So you set up the new window…

newWindow = Titanium.UI.createWindow('app://sample.html');
newWindow.setHeight(600);
newWindow.setWidth(800);
newWindow.setResizable(false);
doyay = setInterval("checkURL()",1000);
newWindow.open();

The variable doyay sets up a repeating event. Every second or so, it runs the checkURL() function until it's told to unset the interval.

function checkURL(oldval){
  var teddy =  newWindow.getWindow();
  if(teddy.document.URL == '[the url you want it to equal]'){
    clearInterval(doyay);
    // run a function that processes the data from the window.
   }
}

AFAIK, Titanium.UI.WebView.url should deliver this same data. The API actually says the value changes as the webview is navigated by internal links. But in Desktop, using GetURL on a child window from the parent/master window doesn't necessarily provide the window's current URL.

One caveat, that window object can be pretty large, so if you're trying to be memory conscious, be careful with it.

— asked April 18th 2010 by Greg Bulmash
  • childwindow
  • geturl
  • getwindow
1 Comment
  • Thanks for this! Unfortunately getURL still hasnt been fixed yet. Does anyone have any other possible workarounds for this?

    — commented November 28th 2011 by Gaurav DCosta

0 Answers

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.