?
Solved

Cookie problem in Safari browser

Posted on 2007-12-03
4
Medium Priority
?
2,690 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 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

How to Create Failover DNS Record Sets in Route 53

Route 53 has the ability to easily configure DNS record sets specifically for failover scenarios. These failover record sets can be configured to failover to full-blown deployments in other regions or to a static HTML page that informs your customers of the issue.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Suggested Courses

770 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