2

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

Community
  • 1
  • 1
Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
  • 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 Aug 08 '14 at 08:50
  • Ultimate goal is to [retrieve references to child window if the parent refreshes](http://stackoverflow.com/questions/25177076/retrieving-references-to-child-window-if-the-parent-refreshes). – Chankey Pathak Aug 08 '14 at 08: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 Aug 08 '14 at 09:02
  • That's just the title of question. It got cleared after your comment 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 Aug 08 '14 at 09: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 -> **http://jsfiddle.net/adeneo/w103ruy7/** – adeneo Aug 08 '14 at 09:08
  • @adeneo - Correct, that's what I meant with finding a different approach to the original problem. (BTW, it doesn't actually work in Chrome because that browser opens each tab in a different thread.) – Álvaro González Aug 08 '14 at 09:15
  • It works for me in Chrome, but it opens a new window, not a tab, could be browser settings ? – adeneo Aug 08 '14 at 09:17
  • Well, yeah, it opens tabs in new windows for me as well, but that's not the issue. It opens a new one every time, rather than reusing existing ones. – Álvaro González Aug 08 '14 at 09:23

2 Answers2

6

First 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.

nemesisdesign
  • 8,159
  • 12
  • 58
  • 97
Stephan
  • 61
  • 1
  • 2
  • Just make sure you pass an empty string into the URL and not a null value as null values will trigger a page refresh on the popup. – TomDotTom Feb 24 '15 at 12:30
3

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360