Titanium Community Questions & Answer Archive

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

fireEvent from webview to window


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?


— asked December 7th 2010 by Nicholas Thibault
  • fireevent
  • iphone
  • webview
1 Comment
  • Can you post your code? The sample you have above looks like it comes from an answer I provided

    — commented December 7th 2010 by Aaron Saunders

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){

    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 ;)

    — answered December 23rd 2010 by Kosso
    1 Comment
    • With var searchView = Ti.UI.currentWindow; this code doesn't work… any solution?

      — commented August 4th 2013 by henry manco
  • 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>
    — answered December 7th 2010 by Aaron Saunders
  • 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.

    — answered December 7th 2010 by Nicholas Thibault
  • 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

    — answered December 23rd 2010 by Peter janett
    1 Comment
    • please do. have a look at this: https://developer.appcelerator.com/question/179463/how-to-get-html-content-remotely-and-open-localy

      — commented December 5th 2014 by David Benko
  • 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.

    — answered December 23rd 2010 by Nicholas Thibault
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.