Dario Rusignuolo
Dario Rusignuolo

Reputation: 2186

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

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?

Upvotes: 0

Views: 2478

Answers (4)

Sam Bellerose
Sam Bellerose

Reputation: 1812

I used this and it worked

window.parent.TiApp.fireEvent

Upvotes: 0

Dario Rusignuolo
Dario Rusignuolo

Reputation: 2186

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!**

Upvotes: 2

Dario Rusignuolo
Dario Rusignuolo

Reputation: 2186

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.*".

Upvotes: 0

bguidolim
bguidolim

Reputation: 367

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.

Upvotes: 0

Related Questions