Titanium Community Questions & Answer Archive

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

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.

— asked May 7th 2010 by Alper Dincer
  • programming
  • webview
0 Comments

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;
    
    — answered March 17th 2011 by Joe iEntry
    permalink
    1 Comment
    • And if mywin.js modifies the variable, how to make it available to other windows ?

      — commented April 1st 2011 by xavier villamuera
  • 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

    — answered May 7th 2010 by Dan Tamas
    permalink
    0 Comments
  • 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 :)

    — answered May 9th 2010 by Alper Dincer
    permalink
    1 Comment
    • hey, how can you pass the variable to a window. I am trying but it keeps telling me that the variable does not exist

      — commented November 25th 2010 by Khang Vo
  • 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
    
    — answered January 5th 2014 by David Glass
    permalink
    0 Comments
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.