I am currently using javascript and am trying to pass sessionGUID via a cookie to a recently opened window using IE9. I want to avoid entering sessionGUID in querystring.
I use the following code to open a new window and assign the cookie to a 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 does not seem to be set when the (document) .ready is executed in a 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) warning after calling getCookie, sGUID is undefined, however the delay (user record) caused by the warning allows me to read the cookie after that.
How can I make sure the cookie is set in a new window before $ (document) .ready is executed? ... or at least ensure that the web service is not called before the sessionGUID is retrieved from the cookie?
The problem does not exist in FireFox or Chrome-just IE.
Thank you in advance for your attention ...
UPDATE (20121115): This link http://ellislab.com/forums/viewthread/220241/ indicates that cookies may not be available until a page request is made. The scenario described above usually occurs only during the initial login or after the removal of cookies (and still intermittent). My current solution / workaround is to open and close a time window and re-extract the cookie. Since the problem occurs when a new window opens in any case, the additional flickering of the window is invisible. I also moved the extraction of the cookie outside of 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"); }