/    Sign up×
Community /Pin to ProfileBookmark

Cookies Issues

Hi, I have an page with some JS that switches tabs, I want to set a JS cookie so that when the user reloads the page the tab option that had selected starts off open.

Here is the JS code i have

[code=php]
<script type=”text/javascript” charset=”utf-8″>
function swapDivs(id,caller,button,buttonOff) {
document.getElementById(caller).style.display = ”;
document.getElementById(id).style.display = ‘none’;
document.getElementById(button).style.background = ‘#ed7819’;
document.getElementById(buttonOff).style.background = ‘#829189’;
createCookie(‘hideDiv’,caller,’1′);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = “; expires=”+date.toGMTString();
}
else var expires = “”;
document.cookie = name+”=”+value+expires+”; path=/”;
}

function readCookie(name) {
var nameEQ = name + “=”;
var ca = document.cookie.split(‘;’);
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
</script>
[/code]

The tab switching works fine, but I cant seam to get the cookie to be remembered. or found.

Here is the section that calls the open tab, and hides the other one.

[code=php]
<script type=”text/javascript” charset=”utf-8″>
var x = readCookie(‘hideDiv’)
if (x) {
createCookie(‘hideDiv’,’orphanDiv’,’1′); // Hide this one be default, if no cookie is set.
}
document.getElementById(readCookie(‘hideDiv’)).style.display = ‘none’;
</script>
[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WebnerdOct 01.2007 — if (x==null) {

Small logic issue should fix it
Copy linkTweet thisAlerts:
@SheldonauthorOct 01.2007 — I tried that, but it still doesn't work when i refresh that page, it always shows the original tab.

Any more ideas?
Copy linkTweet thisAlerts:
@SheldonauthorOct 02.2007 — any one?
×

Success!

Help @Sheldon 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.24,
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,
)...