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
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.
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.
Related
I want to build my own analytic and I need to know from where the requests are coming from with only javaScrpt, I can't believe that the browser is not holding somewhere in window object a variable about from where the request came from. It looks like there is no information in the net or I am not asking the right question.
I hope somebody met this problem before and has a solution :) Thanks!
You can use document.referrer
Syntax
var referrer = document.referrer;
Value
The value is an empty string if the user navigated to the page directly (not through a link, but, for example, by using a bookmark). Because this property returns only a string, it doesn't give you document object model (DOM) access to the referring page.
Inside an <iframe>, the Document.referrer will initially be set to the same value as the href of the parent window's Window.location.
I am launching a pop up window using javascript. This is a security question regarding cross domain messaging between a pop up window and its parent window using javascript "postMessage()".
The html file for the pop up window resides on AWS. The parent window sits on a different domain.
So, a user visits the parent window, and on button click, the pop up window is launched from AWS. Then, the pop up window communicates with the parent window via "postMessage()".
In the parent window, I set the domain to receive messages only from this url: https://s3.amazonaws.com like so:
if(e.origin != 'https://s3.amazonaws.com') {
return;
}
If you can answer any of these questions, it would be helpful. Thank you very much. Does this mean all files from this url will be able to send messages to my parent window? Is this a security risk? Is there a way to specify that messages should only be accepted if they originate from my specific HTML file on AWS? Should I try to host the pop up html file on an internal server that is owned and maintained by my employer?
Thank you.
Yes, the message event can listen for messages from different browsing contexts. It is the prerogative and responsibility of the developer to provide adequate checks as to the origin of the message and specific values of the message which should be expected.
One option is to set the name of the opened window to a unique string, pass the name to window.opener and check if the name is equal to the unique name provided to window.open()
I need to pass JSON object in new window which is opened using window.open()
I have requirement to display stored data in database into new window. At the same time i need to pass a JSON object which is available in current open window to this new window.
Possible ways I had :
Can save json in cookie before open new window, and read it in
ngOnInit() then remove it from cookie.
Can pass required data in URL, but i have large JSON object
Please guide if we can do this by any another approach.
Thank you.
You can use localStorage too instead of cookies. I don't think you have better options if you have to use window.open().
To keep each component functionality self-contained I would use a service to get the json based on the id.
I think you can use Window.postMessage. window.open will return a Window object that you can call postMessage on; in the new window, you'll want to listen for the "message" event on the global window object.
Note that IE8 and IE9 don't support it for windows, only (i)frames.
Instead of cookies or localstorage, how about you use the window object you get when open the window.
var opened_window = window.open('someurl_here');
opened_window.json_data = JSON.stringify(this.local_json_data);
In the child window, you can access the data as part of your window object.
//Inject your window into this component (#Inject('Window') window maybe)
this.new_json_data = JSON.parse(this.window.json_data);
I think this should work.
I want to communicate between two different tabs in the same browser (on the same domain).
When a specific event fires in the second tab, I change a localstorage variable value (using Javascript). I want to detect this change on the first tab. The variable is named "status" and the value changes from 0 to 1
I was thinking for a possible solution and I think that using a timer on the first tab will work, but I think also that there must be a better way.
Do you know if there is any way to detect when the "status" variable value changes without using a timer?
Thanks a lot!
It seems that Storage events should provide what you need.
The HTML spec on storage events ( http://dev.w3.org/html5/webstorage/#the-storage-event )
says :
The storage event is fired when a storage area changes, as described
in the previous two sections (for session storage, for local storage).
When this happens, the user agent must queue a task to fire an event
with the name storage, which does not bubble and is not cancelable,
and which uses the StorageEvent interface, at each Window object whose
Document object has a Storage object that is affected.
Note the final clause, all windows which have access to the storage should be notified of any change.
However note the following clause in previous section of the spec :
When the setItem(), removeItem(), and clear() methods are called on a
Storage object x that is associated with a session storage area, if
the methods did something, then in every Document object whose Window
object's sessionStorage attribute's Storage object is associated with
the same storage area, other than x, a storage event must be fired, as
described below.
So the document which provoked the change is not alerted.
There is definitely a better way -- using events built into the localstorage mechanism. When you update localstorage in tab2, tab1 will get an update event which you can then use as a signal to read the new value from localstorage.
For more info, see:
Bug with Chrome's localStorage implementation?
http://diveintohtml5.info/storage.html
I have a form post method, which is used to show a new page. Its done this way so that the arguments used cannot be seen in the location bar.
Each window is given a unique name, but I want to be able to detect if this browser window is already open, so that calling this form again will not force that current browser window to auto-fresh.
Any suggestions?
I assume you're opening new windows in Javascript. So, assign a variable name to your new window (e.g. var newWin1 = window.open(...))
Then test to see if the document of your window exists:
if(newWin1.document) { alert("Window is open!"); }
else { alert("Window is gone!"); }
For a security note: people can still see the post data you're sending with any HTTP header tool. Check out LiveHTTPHeaders for Firefox (or a million others) to see what I mean.
Another note: Not sure what you're doing, but people don't like when a webpage does things without them asking it to (like opening windows). You may want to consider improving your design to a more user-friendly method.