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.
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.
-
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. :)
-
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.
-
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!
-
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.