Global variable between windows
Hi,
I have 4 tabs and 4 windows for each tab. There is also a another window for extra information. Each window is defined in different Javascript file. One window has a webview. I just want to access this webview from other windows to run evalJS function, but I can't access it.
I try to add a global variable in app.js and send this variable as a parameter for windows, but I can't use it between windows.
Is there any solution for accessing webview from different windows?
Alper.
4 Answers
-
####Assign the variable to the window
To make a variable available to a window you are opening, say from app.js, you can assign the variable to the window before opening it:
var win = Titanium.UI.createWindow({url: 'mywin.js'}); win.myvar = 'my val';
or
var win = Titanium.UI.createWindow({ url: 'mywin.js', myvar: 'my val' });
####Access the variable as an object of the current window
And then you can access the value in the mywin.js by:
var win = Titanium.UI.curentWindow; var localvar = win.myvar;
-
I think the only way is to trigger an event in the webview
take a look at the 3rd reply - mine too - in this post
http://developer.appcelerator.com/question/10671/webview-dom
-
Thanks Tamas., but I found the solution in different way.
I define webview variable in app.js and pass it as variable to all windows. Then use it as a window parameter. It works :)
-
If you create it as a reference type in a Require.js module, expose it via "exports", any other component/file/module using it will have access to it, ie:
code in GPSHelpsers.js
var lastCoordinates = { lastLat: null, lastLong: null }; exports.lastCoordinates = lastCoordinates;
code in other .js file…
var gpsHelpers = require("helpers/GPSHelpers"); Ti.API.info("Last Coordinates >>>>>", gpsHelpers.lastCoordinates.lastLat, gpsHelpers.lastCoordinates.lastLong);
Outputs…
Last Coordinates >>>>> 123.4567 76.54321