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

大兔子大兔子 提交于 2019-12-11 18:08:49

问题


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:


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.



来源:https://stackoverflow.com/questions/26672326/ti-app-fireevent-not-working-error-uncaught-typeerror-cannot-read-property-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!