Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

webView dynamic html fireEvent wont work

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 September 15th 2010 by Matt Hebs
  • dynamic_html
  • fireevent
  • webview
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 October 30th 2010 by Aaron Saunders
  • I can get this to fire in simulator but not on actual device. Tried remote and local html files =(

    — commented February 5th 2012 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 December 11th 2010 by Kosso
    permalink
    3 Comments
    • seems to need lower case html/body tags to make it work…

      — commented July 13th 2012 by Jeff Antram
    • Thanks, its funny solution.It works

      — commented October 15th 2012 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 March 25th 2013 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. :)

    — answered June 9th 2011 by Madhava Jay
    permalink
    0 Comments
  • 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 September 16th 2010 by Matt Hebs
    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 October 30th 2010 by Aaron Saunders
  • any updates on this? did you get this working?

    — answered December 11th 2010 by CallMePaul
    permalink
    0 Comments
  • @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!

    — answered October 5th 2011 by Bernardo Oliveira
    permalink
    0 Comments
  • 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.

    — answered September 16th 2010 by Dan Tamas
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.