webView dynamic html fireEvent wont work

You must Login before you can answer or comment on any questions.

I'm LOSING MY MIND!!!! Im creating a webview from html that is generated. On each of the images i want to attach a onClick which will fire a Ti.App.fireEvent. But it doesn't work, I can call alert(3); that works but as soon as i try to talk to my app nothing happens. using iPad!

here is some code app.js -- build the html and put it in the webview

Ti.App.addEventListener('playVideo', function(e) 
{ 
    Ti.API.info('clicked');
 
});
 
 
        var html = '<HTML><body>';
 
        var css = '<style>div {display:inline-block;text-align:center;width:200px;padding:4px;vertical-align:top;}a{display:block;}</style>';
 
        var script = "<script>function playVideo(vid){Ti.App.fireEvent('playVideo',{data:vid});}</script>";
 
        html += script + css;
 
        for(var i = 0 ; i < dirList.length ; i++){
 
            var title = dirList[i].replace(/.mp4/,'').replace(/_/gi,' ');
            var image = 'small_'+dirList[i].replace(/mp4/,'png');
 
            if(dirList[i].search(/mp4/) > 0 && dirTest.exists(image)){
 
                    html += '<div><img onClick="playVideo(\''+dirList[i]+'\');" src="app://Resources/'+path+'/'+image+'" /><a href="#">'+title+'</a></div>';
 
            }
        }
 
        html += '</body></HTML>';
        Ti.API.info(html);
 
 
        var webView = Ti.UI.createWebView({
 
            top:0,
            html : html
 
        });
 
        SplitViewNav.detailWindow.add(webView);
the WebView gets populated fine but the fireEvent doesn't do jack.

— asked 3 years ago by Matt Hebs
2 Comments
  • did you ever get this to work? I spent about 8 hours researching this yesterday and got NOTHING to work... The event never gets back to my application.

    — commented 3 years ago by Aaron Saunders

  • I can get this to fire in simulator but not on actual device. Tried remote and local html files =(

    — commented 1 year ago by Ryan Hickman

6 Answers

That's the way to it (it works for me) but it look like your html is invalid.

Start by keep all your tags lowercase (I see you have HTML in upper) - not saying that it shouldn't work with that...

but also, I see you are missing a <head> tag

Put your script and style tags inside the head tag before the opening body.

That *might solve it.

Let us know if it doesn't, because I use an identical approach to deal with clicks on urls in my webviews.

— answered 2 years ago by Kosso .
answer permalink
3 Comments
  • seems to need lower case html/body tags to make it work...

    — commented 11 months ago by Jeff Antram

  • Thanks, its funny solution.It works

    — commented 8 months ago by nikhil patil

  • Good grief-- I can't believe that was it. How weird. Took me hours before I found this post. Thanks. To recap- you must have <html><head> and <body> tags and in the right places.

    — commented 2 months ago by Richard Peacock

Wow this was driving me nuts! Turns out if you dont put your tag inside a proper <html><head> and <body> tagged document, it wont communicate with the Titanium javascript name space.

So make sure you format your document right and it will work well. :)

Try this:

function playVideo(vid){Titanium.App.fireEvent('playVideo',{data:vid});}
I'm not sure Ti is available in the webview scope. Actually I never use it.

If you search for it that is how you talk from the webview to the app. I've looked at other peoples working examples and they seem to be fine. But they were all using the iPhone.

If its not available how do you communicate from inside webview to the app.

I know you can do it with the iOS sdk.

— answered 3 years ago by Matt Hebs
answer permalink
1 Comment
  • Can you post a link to other peoples examples that actually work? I found alot of talk but no code that actually works.

    — commented 3 years ago by Aaron Saunders

any updates on this? did you get this working?

@Madhava Jay could you please provide us with some code, please?

Like Aaron Saunders, I've been trying to get this to work for a while but haven't had success yet.

Also haven't seen any code that works so far. Could you please shed a light?

Thanks!

Your Answer

Think you can help? Login to answer this question!