Reputation: 9506
I have to make a web page with two different window and let they share some variables.
When the main window is loaded, the second windows is opened, and here I can store the second window object and access its vars:
Main window
var mainvar=1;
var secondwindow=open('second.htm');
$("button").click(function(){
alert(secondwindow.testvar);
});
My trouble starts when I want to do the oppsite: access main window vars from the second window script:
Second window
var secondvar=2;
var mainwindow;// how can I set this?
$("bunno").click(function(){
alert(mainwindow.testvar);
});
Upvotes: 3
Views: 132
Reputation: 9506
I found this that maybe can fit:
Main window
var mainvar=1;
var secondwindow=open('second.htm');
$("button").click(function(){
alert(secondwindow.testvar);
});
$(secondwindow).load(function() {
secondwindow.mainwindow=window;
});
Second window
var secondvar=2;
var mainwindow=false;
$("bunno").click(function(){
alert(mainwindow.testvar);
});
Upvotes: 1
Reputation: 2961
My suggestion would be to use localstorage to store the variables and make them accessible between windows and sessions.
localStorage.setItem('myValue', '1234');//in window 1
alert(localstorage.myValue)//in window 2
Failing that, cookies could also be used. Not quite as secure, but if you aren't passing secure data between the windows, this would work too.
Upvotes: 1