Passing parameters to HTML page loaded in a webView
I have been working on Titanium for last few days. As a web developer I found it very interesting to develop an iphone and Android app without knowing objectiveC or Java.
One of the problem I faced was to pass parameter to HTML page loaded in a webView.
In this post I will show how to make javascript variable accessible to HTML page opened in a webView.
In the following code, the JS variable ‘message’ is initialized on ‘beforeload’ event of the webview in webview.evalJS() method which will invoke the javascript code inside the context of the webview.
app.js
var window = Ti.UI.createWindow();
var webview = Titanium.UI.createWebView({url:'message.html'});
window.add(webview);
var str = "Hello world!";
webview.addEventListener('beforeload',function(e)
{
webview.evalJS("var message='" + str + "';");
});
window.open();
As the JS ‘message’ variable is assigned before loading the webview, it is also available in the html page.
message.html
<html>
<head>
<script type="text/javascript">
alert(message);
</script>
</head>
<body>
</body>
</html>
Hi Sushma,
Thanks for writing this post which helped me to solve my issue while passing data from Titanium JS to Webview external HTML/JS.
Actually this way of passing the data is working fine only if I’m sending some basic data(strings, numbers etc.), but not allowing me to send some Titanium objects like VideoPlayer.
I’m trying to send a video player object to my HTML like this:
If I try to access that object in the corresponding HTML file like
Ti.API.info(playerObj);
then my application is simply getting stuck without any error message. Have you tried to send any Titanium objects like this?Can you please help me out for sending Titanium objects to the corresponding HTML file using your approach.
Thanks,
Siva...
sivadurgarao
September 24, 2011 at 11:48 am
Hai,
i tried this code,but not working. using events (addEventListener & fireEvent) works fine for me.
Also this beforeload event also not working.i tested in android 2.2 emulator.waiting for the reply.
thanks
vinod
vinod varghese
November 9, 2011 at 12:30 pm