fireEvent from webview to window
Hi,
I'm learning Titanium by developing a simple iPhone app, and it's coming along pretty good. However, I've been trying to figure out how to get fireEvent to work and after checking out different examples I just cannot get it to work (probably missing something silly). All I want to do is when the user clicks a link in a webview, the app.js gets a call and spits up an alert, so:
In app.js:
Titanium.App.addEventListener('fromwebview',function(e)
{
alert('worked');
});
In html file:
<a href="#" onclick="Titanium.App.fireEvent('fromwebview');">Test</a>
If I can get fireEvent working once, I should be able to figure things out from there. Can anyone see what it is I'm doing wrong?
Thanks,
Nick
5 Answers
-
Try this:
in app.js:
Titanium.App.addEventListener('fromwebview',function(e) { Ti.API.info('here is a message from the webview : '+e.msg); });
And then in the HTML <head> tag of your webView add a simple JS function to call the custom event. Something like:
<script type="text/JavaScript"> function link_hit(str){ Ti.App.fireEvent(\'fromwebview\',{msg:str}); } </script>
So, in your webview, if a link in the body had:
<a href="javascript:link_hit('goodbye cruel world')">hello world</a>
you should see the event get hit in the app.js and see the msg in the debug/info window.
Hope this works/helps ;)
-
I see the problem
this code<a href="#" onclick="Titanium.App.fireEvent('fromwebview');">Test</a>
should be like this… you need to pass the options parameter
<a href="#" onclick="Titanium.App.fireEvent('fromwebview', {});">Test</a>
-
I've figured out why I was having so many problems, I was trying to get the alert by calling to a remote server, so most of my code tests do work when tested locally.
Nevertheless, does anyone know how to get the above code working when calling to an HTML file on a remote server? I've spent the last few hours banging my head against the wall trying to get this to work.
-
I can't seem to find the old thread, but I asked this months ago and was told you can only trigger custom events like that from LOCAL web view content. I wrote code to captue each page locally, then load it locally in a web view to get around the issue. If you can't find any example code, let me know and I will try to help find some example code.
Peter Janett
-
Thanks Peter, it took me a while but I found out basically what you said. I decided to go in another direction and I'm using XHR and XML (XML headers with PHP), where I'll send a request to the server requesting the data I want and the data is then outputted as an XML file. It's certainly not elegant, but it gets the job done. It's also given me more experience with Titanium, so I'm seeing other possible ways that might work as well that I hope to eventually test out.