Ti.App.fireEvent - Reference error Ti is not defined

╄→гoц情女王★ 提交于 2020-01-06 19:17:22

问题


I have this simple Titanium js script.

app.js

var win = Ti.UI.createWindow();
    var webview = Ti.UI.createWebView({
        url: 'logging.html'
    });
    webview.addEventListener('help',function(){
        alert('help');
    });
    win.add(webview);
    win.open();

logging.html

<html>
    <body>
        <a onclick="Ti.App.fireEvent('help')">Help</a>
    </body>
</html>

when I click on the Help link, the console gives me Reference Error: Ti is not defined.

I also tried changing Ti with Titanium, but same error.

------------- EDIT ----------

this error comes only with web browser. iOS works perfectly. but

when titanium studio compiles the project for web mobile, I can see titanium.js and TI/* folder, so I guess it can't load Ti object. can anyone explain me why?


回答1:


I found a solution!

simply add to all of your html pages the simple script below

var Ti = window.parent.Ti

have fun!

EDIT:

from sdk version 3.0.2GA on, I guess they fixed it. now it calls Ti sdk without that hack!**




回答2:


First, change:

webview.addEventListener('help',function(){
    alert('help');
});

To:

Ti.App.addEventListener('help',function(){
    alert('help');
});

And second: Call "Ti.App.fireEvent()" without the final "s" in your HTML file.




回答3:


after some tests, I found that the previous code works perfectly on iOS phisical device/simulator and Android.

it doesn't on android web browser emulator and normal mobile browser (Firefox as mobile web app)

so, it seems that Titanium api calls will never work on web browsers because of "normal javascript library doesn't have Titanium.* or Ti.*".




回答4:


I used this and it worked

window.parent.TiApp.fireEvent


来源:https://stackoverflow.com/questions/15368543/ti-app-fireevent-reference-error-ti-is-not-defined

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