I'm opening a window as
winRef = window.open(......);
Then I'm storing the above winRef
in cookie so that I can get the reference to child window even if the parent refreshes.
That didn't work because when I tried to save winRef in cookie it just saves the text representation/string
of the object so you only have "[object Window]"
as string, it's not an object.
Is there any way to store the window reference as a cookie? If it's not possible then what are some other possible methods which I can use?
PS: I think storing just the window name instead of window object in the cookie can solve the issue but it can't be done in my case, I can't provide window names, basically the window is an online editor, if I give a particular name to it then user can't open multiple online editors as it will always reload the currently opened window.
Ultimate goal: Retrieving references to child window if the parent refreshes
I'm opening a window as
winRef = window.open(......);
Then I'm storing the above winRef
in cookie so that I can get the reference to child window even if the parent refreshes.
That didn't work because when I tried to save winRef in cookie it just saves the text representation/string
of the object so you only have "[object Window]"
as string, it's not an object.
Is there any way to store the window reference as a cookie? If it's not possible then what are some other possible methods which I can use?
PS: I think storing just the window name instead of window object in the cookie can solve the issue but it can't be done in my case, I can't provide window names, basically the window is an online editor, if I give a particular name to it then user can't open multiple online editors as it will always reload the currently opened window.
Ultimate goal: Retrieving references to child window if the parent refreshes
Share Improve this question edited May 23, 2017 at 10:27 CommunityBot 11 silver badge asked Aug 8, 2014 at 8:46 Chankey PathakChankey Pathak 21.7k12 gold badges88 silver badges136 bronze badges 8- Of course, cookies are sent as HTTP headers, which are text. You can't store variable references more than you could print them on a poster. What's the ultimate goal? – Álvaro González Commented Aug 8, 2014 at 8:50
- Ultimate goal is to retrieve references to child window if the parent refreshes. – Chankey Pathak Commented Aug 8, 2014 at 8:51
- You answered that other question yourself saying that cookies could not store references and now you ask how to use cookies to store references? I suspect it'd be more productive to try a different approach for the original problem, rather than insisting on the reference dead end. – Álvaro González Commented Aug 8, 2014 at 9:02
-
That's just the title of question. It got cleared after your ment that it's not possible using cookies. I already mentioned in this question that
If it's not possible then what are some other possible methods which I can use?
– Chankey Pathak Commented Aug 8, 2014 at 9:06 -
1
It's actually not that hard, give the window a name, and then just call
window.open
again with that same name, if a window with that name already exists, you'll get a reference to that window, here's a demonstration -> jsfiddle/adeneo/w103ruy7 – adeneo Commented Aug 8, 2014 at 9:08
2 Answers
Reset to default 6First excuse me for my poor English ;-)
A possible workaround for this problem is to set a name in the window.open function (eg: popup = window.open(URL, popup_window, specs, replace)
Then save popup in a cookie.
When retrieving the cookie, you'll get the [object Window]
as you said.
eg: popup = getCookie('popup');
After just do the following :
if (popup == null) {
//No popup
} else {
//Popup exist, retrieving is ref
popup = window.open("" ,"popup_window");
}
Just reuse the window.open
function, just with the same name (popup_window
) and no other arguments, as this window already exist no further actions will be performed just returning the popup_window
ref.
Variables are abstractions that live on primary memory (aka RAM) and in the scope of a running process or thread. You just can't store them anywhere else.
Particularly, cookies are plain text. They are sent as HTTP headers and they're often stored in text files. So to answer your question: no, you cannot store a JavaScript object of type window
in a cookie.