Solved

Cookie problem in Safari browser

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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
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 tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

705 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