2

When I create a child window from my main window, I'd like to pass a JavaScript object ot it, but I'm not sure if there actually is a way to do it?

Two windows created with TideSDK each have their own JavaScript environement, just like two browser windows (and that's just what they are, If I understand it right), so you can't access a variable in one window from another one. On the other hand, you can access other windows from the one you are in (for example with Ti.UI.getOpenWindows). So... is there a way to do it?

There are some workarounds I believe are possible, but none of them is very straightforward, and each uses something other then just plain JavaScript:

  • using Ti.Database or Ti.Filesystem to store the data I want to pass, and then retrieve it from the child window
  • pass the data to the new window as GET variables,example: Ti.UI.createWindow("app://page.html?data1=test&data2=foobar");
Alex
  • 1,157
  • 3
  • 11
  • 25

2 Answers2

4

you can assign the object to the child window object

var objToBePassed = {foo:'bar'};    
var currentWindow = Ti.UI.currentWindow;
var newWindow = currentWindow.createWindow("app://page.html");
newWindow.obj = objToBePassed;
newWindow.open();

and in the javascript environment of the child window you can access the object by

var currentWindow = Ti.UI.currentWindow;
var obj = currentWindow.obj;

another way is to use Ti.API.set:

Ti.API.set('objKey', objToBePassed);

and you can access the object anywhere by

var obj = Ti.API.get('objKey');
Eric
  • 116
  • 3
0

I know this has been answered, but I came accross a way to mimic the PHP $_GET variable, which will do what the OP asked I reckon:

<script type="text/javascript">
(function(){ 
   document.$_GET = [];
   var urlHalves = String(document.location).split('?');
   if(urlHalves[1]){
      var urlVars = urlHalves[1].split('&');
      for(var i=0; i<=(urlVars.length); i++){
         if(urlVars[i]){
            var urlVarPair = urlVars[i].split('=');
            document.$_GET[urlVarPair[0]] = urlVarPair[1];
         }
      }
   }

})();

</script>

And then use it:

<script type="text/javascript">
    var conts = '<li><a  title="back to list" href="/menu.html?module='+document.$_GET['module']+'">Unit Listing</a></li>';
    document.write(conts);
</script>

Works fo me.

Chris King
  • 121
  • 6