Search code examples
javascriptwebviewtitaniumappcelerator-mobile

Send data to external webview


I'm trying to send some data from app.js to an open webview (external url, example: http://mysite.com/file.html), without success. I've check through many questions and answers and tried different solutions with Ti.App.fireEvent and Ti.App.addEventListener without any good success. I did however find a solution that did this with a local html-file some time ago, but weren't able to recreate this for an external.

app.js

Ti.App.fireEvent('helloWorld', { data : "Hello World" );

http://mysite.com/file.html

Ti.App.addEventListener('helloWorld', function(e) 
{   
    // do something with e.data
});

doesn't seem to do anything.


Solution

  • Solved the problem by using evalJS app.js

    web.addEventListener('load', function() {
    
            var data = "some data";
            web.evalJS("testJS('" + data + "')");
    
    });
    

    http://mysite.com/file.html

    <script>
        function testJS (data) {
            alert(data);
        }
    </script>