I am currently using javascript and trying to pass a sessionGUID via a cookie to a newly opened window using IE9. I wish to avoid putting the sessionGUID on the querystring.
I am using the following code to open the new window and assign the cookie to the new window:
var pathname = /msgViewer.htm?A=" + aGUID + "&Detached=yes";
var myWindow = window.open(pathname, "detached_window");
myWindow.document.cookie = "SG=" + sGUID;
However, the cookie doesn’t appear to be set when (document).ready executes in the new window.
$(document).ready(function () {
...
sGUID = getCookie("SG");
...
[call to AJAX webservice that requires sGUID be passed]
...
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else {
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
return unescape(dc.substring(begin + prefix.length, end));
}
When I set a (test) alert after the getCookie call the sGUID is undefined, however the delay (user entry) caused by the alert allows the cookie to be read after that.
How can I ensure the cookie is set in the new window before $(document).ready executes? …or at least ensure the webservice isn’t called before the sessionGUID is retrieved from the cookie?
The issue doesn’t exist in FireFox or Chrome- just IE.
Thanks in advance, for your consideration…
UPDATE (20121115):
This link http://ellislab.com/forums/viewthread/220241/ indicates cookie data may not be available until after a page request has been made. The scenario described above generally only occurs during the initial login or after cookies have been deleted (and still intermittent at that) My current solution/workaround is to open and close a temp window and re-retrieve the cookie. Since the issue happens when a new window is opening anyway, the additional window flicker is inconspicuous. I also moved the retrieval of the cookie outside the $(document).ready function.
Here is the additional code:
sGUID = getCookie("SG");
if (sGUID == null) {
var jwURL = "/Test.htm";
jw = window.open(jwURL, "junk_window",width=1,height=1);
jw.close();
sGUID = getCookie("SG");
}
“Update 20121115” (opening/closing a temp window) did fix my issue. I did ultimately use a different getCookie function to retrieve the cookie- however that doesn’t directly relate to my initial question: