Cookie problem in Safari browser

Posted on 2007-12-03
Last Modified: 2012-05-05
I have a page index.html and have a link on it "Link 1".  The HTML code for this link is <a href="page2.html?seg=setSegXYZ">
On this page2.html, there is a call to the function getStart('PageToOpen.html','CookieName')

Thus, basically on clicking the link "Link 1" on the index page, PageToOpen.html is called by setting the cookie "CookieName"

The cookie is set via a query string.  The exact code for setting the cookie is
document.cookie = name + "=" + escape(style) + "; expires=" + expire.toGMTString();

Here name is the Cookie name.

The problem is that this works fine in IE and Firefox. However, when I open the sam page in Safari, it seems that the cookie does not set and the page kind of loops back and forth (i.e. page X calls page Y, which again calls page X and so on).

As an additional info, I have set my browser to accept all cookies and another interesting thing is that this problem occurs locally, while the same js works fine on the live site(www)

I am not sure where the problem is. Please suggest.

document.cookie = name + "=" + escape(style) + "; expires=" + expire.toGMTString();

Open in new window

Question by:askanyquestions123
  • 2
  • 2
LVL 75

Accepted Solution

Michel Plungjan earned 500 total points
ID: 20396017
Use theattached code

Put it in an external file called cookie.js
// cookie.js file

var cookieToday = new Date(); 

var expiryDate = new Date(cookieToday.getTime() + (365 * 86400000)); // a year

/* Cookie functions originally by Bill Dortsch */

function setCookie (name,value,expires,path,theDomain,secure) { 

   value = escape(value);

   var theCookie = name + "=" + value + 

   ((expires)    ? "; expires=" + expires.toGMTString() : "") + 

   ((path)       ? "; path="    + path   : "") + 

   ((theDomain)  ? "; domain="  + theDomain : "") + 

   ((secure)     ? "; secure"            : ""); 

   document.cookie = theCookie;


function getCookie(Name) { 

   var search = Name + "=" 

   if (document.cookie.length > 0) { // if there are any cookies 

      var offset = document.cookie.indexOf(search) 

      if (offset != -1) { // if cookie exists 

         offset += search.length 

         // set index of beginning of value 

         var end = document.cookie.indexOf(";", offset) 

         // set index of end of cookie value 

         if (end == -1) end = document.cookie.length 

         return unescape(document.cookie.substring(offset, end)) 




function delCookie(name,path,domain) {

   if (getCookie(name)) document.cookie = name + "=" +

      ((path)   ? ";path="   + path   : "") +

      ((domain) ? ";domain=" + domain : "") +

      ";expires=Thu, 01-Jan-70 00:00:01 GMT";


Open in new window

LVL 75

Expert Comment

by:Michel Plungjan
ID: 20396086



var val = getCookie("Cookiename")

Author Comment

ID: 20397414
ok..Thanks I will use this code and get back to you, if I have any queries.

Author Closing Comment

ID: 31412291
Thanks a lot..Very helpful answer.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now