0
votes

I have a simple Titanium app which opens a webview, and loads a URL. I want the links to open in the default browser, not the app.

I have some jQuery code which adds a click event to all links in the page.

<script>
jQuery('a').click(function() {
    console.log("Handler for .click() called for URL: " + this.href);
    Ti.App.fireEvent('ynOpenURL', {url:  this.href });
});

In the app.js file I have:

var win = Ti.UI.createWindow();

var mywebview = Ti.UI.createWebView({
    url :  'http://xxxx.xxxxx.com/',
    enableZoomControls: false   
});

win.add(mywebview);
win.open();
Ti.App.addEventListener('ynOpenURL', function(e) {
 Ti.API.info('yyyyyy');
 Ti.API.info(e.url);

 if (Ti.Platform.canOpenURL(e.url)){
  Ti.Platform.openURL(e.url);
   } else {
    alert("Cannot open URL");
 }

When I run the app on my Android (4.4) the web page loads correctly.

When I click a link on the phone I get the following in the console:

Handler for .click() called for URL: http://xxx.xxxxx.xxx/
Uncaught TypeError: Cannot read property 'App' of undefined (xxxx)

None of the Titanium console events are logged, only the javascript events in the webpage.

1
from where is the webview part loaded from, remote server url or html file within app ?turtle
the webview is loading a remote URLWilliam Macdonald

1 Answers

2
votes

As you mentioned that the webview is loading remote url. So Ti.* or Titanium.* are not available at your remote server.

What you can do is :

  • Create a html file within the app and load the data via ajax and use fireEvent.
  • Load the data via titanium http client and use Ti.Platform.openURL accordingly.

Edit : Quote from Titanium WebView Docs

Remote Scripts

Scripts downloaded from remote web servers cannot access the Titanium namespace.

To interact with remote content, wait until the content is loaded, then use the evalJS method to execute a JavaScript expression inside the web view and retrieve the value of an expression.