Solved

Cookie problem in Safari browser

Posted on 2007-12-03
4
2,657 Views
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

0
Comment
Question by:askanyquestions123
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
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

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20396086
Use:

setCookie("Cookiename","CookieValue",expiryDate)

and

var val = getCookie("Cookiename")
0
 

Author Comment

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

Author Closing Comment

by:askanyquestions123
ID: 31412291
Thanks a lot..Very helpful answer.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

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.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

825 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