Titanium Community Questions & Answer Archive

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

Titanium.Platform.openURL not working with a variable

I've got the following in an attempt open a url in Safari.

//open in safari button
var safariBtn = Titanium.UI.createButton({
     title:"open in safari"
});
safariBtn.addEventListener('click', function() { 
    Titanium.Platform.openURL(rowLink); 
});

The button lives in the rightNavButton placement in the window bar. The variable 'rowLink' is a legit, working variable as it is also being used to load the webview below. Right now the button does nothing, but if I were to replace the variable with a string like "http://www.google.com" it functions just fine. I'm banging my head against a wall trying to figure out why a variable that works with a webview refuses to work as the value in openURL. Anyone see anything missing?

— asked May 13th 2010 by Russell Morgan
  • titanium.platform.openurl
0 Comments

5 Answers

  • rowLink needs to be defined out of the click function to be available
    try this:

    
    var rowLink = "http://google.com";
    
    safariBtn.addEventListener('click', function() { 
        Titanium.Platform.openURL(rowLink); 
    });
    
    — answered May 13th 2010 by Dan Tamas
    permalink
    0 Comments
  • Yeah the variable is defined outside the click function. The value of rowLink is being extracted from twitter via a regex operation. I changed that regex value to a simple string and the whole thing worked so it looks like that regex is doing something that the webview.url method doesn't much care about but openURL does. I've looked at the str and it seems ok to me so now I guess I have to deal with regex, which isn't going to be fun :-(

    — answered May 13th 2010 by Russell Morgan
    permalink
    0 Comments
  • put a

    Titanium.API.info( result_of_regex )
    

    to see what is there

    — answered May 13th 2010 by Dan Tamas
    permalink
    0 Comments
  • rowLink must contain a valid URL.

    — answered May 14th 2010 by Peter Lum
    permalink
    0 Comments
  • Titanium.API.info( result_of_regex ) comes back with what looks like a properly formatted string and further upstream in this app I'm already removing any null values. Even weirder if I cut and paste that value from the log window and use it as a static variable (instead of my google.com example) it again works. I'll be honest, regex gives me a headache so I'm using the following that I found online:

    //link extractor
    function getLinks(text) {
      var exp = /(b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig;
      return text.match(exp); 
    }
    

    If anyone can see a flaw there that might be causing this issue I'd be super appreciative. If i can't sort this out I'm just going to have to figure out another way of letting the user open safari.

    — answered May 14th 2010 by Russell Morgan
    permalink
    1 Comment
    • @Russell Morgan Was this on Android? Still a problem?

      — commented July 18th 2012 by Eduardo Gomez
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.