【问题标题】:Ti.App.fireEvent not working; error Uncaught TypeError: Cannot read property 'App' of undefinedTi.App.fireEvent 不工作;错误未捕获的类型错误:无法读取未定义的属性“应用程序”
【发布时间】:2014-10-31 10:21:28
【问题描述】:

我有一个简单的 Titanium 应用程序,它打开一个 web 视图并加载一个 URL。我希望链接在默认浏览器中打开,而不是在应用程序中打开。

我有一些 jQuery 代码向页面中的所有链接添加点击事件。

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

在我的 app.js 文件中:

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");
 }

当我在我的 Android (4.4) 上运行该应用程序时,网页会正确加载。

当我点击手机上的链接时,我会在控制台中看到以下内容:

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

不记录任何 Titanium 控制台事件,只记录网页中的 javascript 事件。

【问题讨论】:

  • 从哪里加载 webview 部分,远程服务器 url 或 app 内的 html 文件?
  • webview 正在加载远程 URL

标签: javascript android jquery titanium appcelerator


【解决方案1】:

正如你提到的,webview 正在加载远程 url。所以 Ti.*Titanium.* 在您的远程服务器上不可用

你可以做的是:

  • 在应用程序中创建一个 html 文件并通过 ajax 加载数据并使用fireEvent
  • 通过titanium http client加载数据并相应地使用Ti.Platform.openURL

编辑:引用Titanium WebView Docs

远程脚本

从远程 Web 服务器下载的脚本无法访问 Titanium 命名空间。

要与远程内容交互,请等待内容加载完毕,然后使用 evalJS 方法在 Web 视图中执行 JavaScript 表达式并检索表达式的值。

【讨论】:

    猜你喜欢
    • 2013-07-23
    • 2015-07-11
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2021-12-22
    相关资源
    最近更新 更多