/    Sign up×
Community /Pin to ProfileBookmark

Cookie code not work in Firefox

Can someone tell why Firefox does not support this cookie code, and how do i fix it so it supports.

By the way works in IE 6.

The code is leeched from [url]http://www.netspade.com/articles/javascript/cookies.xml[/url]

[CODE]/**
* Sets a Cookie with the given name and value.
*
* name Name of the cookie
* value Value of the cookie
* [expires] Expiration date of the cookie (default: end of current session)
* [path] Path where the cookie is valid (default: path of calling document)
* [domain] Domain where the cookie is valid
* (default: domain of calling document)
* [secure] Boolean value indicating if the cookie transmission requires a
* secure transmission
*/
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + “=” + escape(value) +
((expires) ? “; expires=” + expires.toGMTString() : “”) +
((path) ? “; path=” + path : “”) +
((domain) ? “; domain=” + domain : “”) +
((secure) ? “; secure” : “”);
}

/**
* Gets the value of the specified cookie.
*
* name Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
* or null if cookie does not exist.
*/
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));
}

/**
* Deletes the specified cookie.
*
* name name of the cookie
* [path] path of the cookie (must be same as path used to create cookie)
* [domain] domain of the cookie (must be same as domain used to create cookie)
*/
function deleteCookie(name, path, domain)
{
if (getCookie(name))
{
document.cookie = name + “=” +
((path) ? “; path=” + path : “”) +
((domain) ? “; domain=” + domain : “”) +
“; expires=Thu, 01-Jan-70 00:00:01 GMT”;
}
}[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehSep 16.2005 — Try setting practical default values. For example:
[code=php]function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "/") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "0");
}[/code]
Copy linkTweet thisAlerts:
@_JZ_authorSep 19.2005 — thx, but it only works partially.
Copy linkTweet thisAlerts:
@MjhLkwdSep 19.2005 — JZ:

This works in FF & IE:

[CODE]<HTML>
<Head>
<Script Language=JavaScript>

var expDate = new Date();
expDate.setTime(expDate.getTime()+365*24*60*60*1000); // one year

function setCookie(isName,isValue,dExpires){

document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}

function getCookie(isName){

cookieStr = document.cookie;
startSlice = cookieStr.indexOf(isName+"=");
if (startSlice == -1){return false}
endSlice = cookieStr.indexOf(";",startSlice+1)
if (endSlice == -1){endSlice = cookieStr.length}
isData = cookieStr.substring(startSlice,endSlice)
isValue = isData.substring(isData.indexOf("=")+1,isData.length);
return isValue;
}

function dispCookie(isName){

nValue = getCookie(isName);
alert(nValue);
}

function deleteCookie(isName){

if (getCookie(isName)){document.cookie = isName + "="+
"; expires=Thu, 01-Jan-70 00:00:01 GMT";}
}

</Script>
</Head>
<Body>
<input type=button value='Set 1st Cookie' onclick="setCookie('anyName','hello',expDate)">
&nbsp;<input type=button value='Set 2nd Cookie' onclick="setCookie('someName','goodbye',expDate)">
<br><br>
<input type=button value='Delete 1st Cookie' onclick="deleteCookie('anyName')">
&nbsp;<input type=button value='Delete 2nd Cookie' onclick="deleteCookie('someName')">
<br><br>
<input type=button value='Read 1st Cookie' onclick="dispCookie('anyName')">
&nbsp;<input type=button value='Read 2nd Cookie' onclick="dispCookie('someName')">
</Body>
</HTML>[/CODE]
×

Success!

Help @_JZ_ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...