简体   繁体   中英

Ti.App.fireEvent not working; error Uncaught TypeError: Cannot read property 'App' of undefined

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM