Can't access variables from a web view
Hi,
I've just started out using Titanium and it is a great tool.
Just having a bit of an issue passing a var from a button click to a webview
Here is the code that listen for a click on a button and opens the webview. You can see i set a variable called 'justtesting' to the web view (employerID is definitely set, i have double checkeded)
b1.addEventListener('click', function(e)
{
var w = Ti.UI.createWindow();
w.orientationModes = [
Titanium.UI.PORTRAIT,
Titanium.UI.LANDSCAPE_LEFT,
Titanium.UI.LANDSCAPE_RIGHT
];
var webview = Ti.UI.createWebView();
webview.url = 'profile.htm';
webview.justtesting = employerID;
w.add(webview);
Titanium.UI.currentTab.open(w,{animated:true});
});
Then i have the following HTML file. The #foo div gets it content changed, but the alert doesn't fire.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
</head>
<body id="mybody" style="height:500px;background-color:#999">
Hello from local webview. You should see an indicator in the middle and my text should be blue.
We are also including JQuery
<div id="foo" style="margin-top:20px;font-weight:bold">hello</div>
<script>
$('#foo').html('i have changed');
alert(Ti.UI.currentWindow.justtesting);
</script>
</body>
</html>
Any ideas
6 Answers
-
Accepted Answer
i solved this by firing an event when the webview is finished loading.
so, in your parent js file:
var webview1 = Titanium.UI.createWebView({url:'somePage.html'}); webview1.addEventListener('load', function() { Ti.App.fireEvent('pageReady',{id:1}); }); Ti.UI.currentWindow.add(webview1);
then you can then listen for this event inside the web view and get the variables sent along with the event
<script type="text/javascript"> Ti.App.addEventListener('pageReady',function(e) { alert('the id is' + e.id); }
-
i solved this by firing an event when the webview is finished loading.
so, in your parent js file:
var webview1 = Titanium.UI.createWebView({url:'somePage.html'});
webview1.addEventListener('load', function() {
Ti.App.fireEvent('pageReady',{id:1});
});Ti.UI.currentWindow.add(webview1);
then you can then listen for this event inside the web view and get the variables sent along with the event
<script type="text/javascript">
<!–
Ti.App.addEventListener('pageReady',function(e)
{
alert('the id is' + e.id);}
-
i solved this by firing an event when the webview is finished loading.
so, in your parent js file:
var webview1 = Titanium.UI.createWebView({url:'somePage.html'});
webview1.addEventListener('load', function() {
Ti.App.fireEvent('pageReady',{id:1});
});Ti.UI.currentWindow.add(webview1);
then you can then listen for this event inside the web view and get the variables sent along with the event
<script type="text/javascript">
<!–
Ti.App.addEventListener('pageReady',function(e)
{
alert('the id is' + e.id);}
-
You might want to try attaching 'justtesting' to your window object rather than the webview object.
w.justtesting = employerID;
Worth noting that the alert in your HTML is attempting to indicate exactly that.
-
i Tried the same thing
var mainDashboardWebView = Ti.UI.createWebView();
mainDashboardWebView.url =mainDashboard.html;
mainDashboardView.add( mainDashboardWebView );
mainDashboardWebView.addEventListener('load', function() { Ti.App.fireEvent('pageReady',{id:1}); });
//web view <script type="text/javascript"> Ti.App.addEventListener('pageReady',function(e) { alert('the id is' + e.id); } </script>
No alert // :( -
As of fully updated Titanium 20th July 2013, the above code does not work - well, it sort of does… If I attempt to do anything with the variable in the web view, for example, display it in an alert , the alert displays with the correct information in it, but then the system hangs up and I can't clear the alert box.
I have attempted several methods of sending variables to a webview, but, they all demonstrate the 'hang' as does the Titanium 2 demo code at https://wiki.appcelerator.org/display/guides/Communication+Between+WebViews+and+Titanium - I would expect that, but i have not found a working Titanium three method yet.
Please help ! Thanks