PDA

View Full Version : JS Cookie saving & IE



Wrathi
Mar 21st, 2007, 05:01 PM
Hi again all!

I'm having some trouble with one of my websites. I have a cascading menu made through Javascript and PHP.

The basic idea is that:

1 - The full menu is shown
2 - The user clicks to make the menu smaller
3 - Javascript makes the menu smaller
4 - Javascript saves its menu state into a Cookie.

I then use PHP to read the cookie and output the correct state upon pageload - so that the state is saved.

This works absoutely 100% fine in FireFox. However, this does not work at all in IE. The menu contracts when clicked, but simply does not save the cookie. I looked into my temp internet files and the cookie is not created at all.

I have also run this code through JSLint, and it returns no major errors.

Internet Explorer does not give me any errors either. It just doesn't work.

Now, on to the code:



//Get all table rows
var myspans = [], spans = document.getElementsByTagName('tr');
//Declare some variables
var myspan, dr = 0;
//If we're using IE
if(navigator.appName.search('MSIE') > -1){
//We want visible rows to be "block"
displayType = "block";
}else if(navigator.appName.search('Microsoft Internet Explorer') > -1){
//For newer versions of IE
displayType = "block";
}else{
//Otherwise, it'll be a table-row.
displayType = "table-row";
}
//Declares some variables from PHP. This is simply "1", "2", or "3". If the cookie is empty, the variables are automatically defaulted to "1".
var statsCase = <?=$CKStats?>;
var drCase = <?=$DRStats?>;

//function to cascade my first menu
function toggleStats(){
//Increase the state
statsCase+=1;
//If we can't cascade anymore, expand it back to full.
if(statsCase > 3){ statsCase = 1; }

//If we want a full menu
if(statsCase == 1){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st1'){
myspan.style.display = displayType;
}
if(myspan.id == 'st2'){
myspan.style.display = displayType;
}
}
document.getElementById('st3').src = 'images/contract.gif';
//Otherwise, if we want a half-menu
}else if(statsCase == 2){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st1'){
myspan.style.display = displayType;
}
if(myspan.id == 'st2'){
myspan.style.display = 'none';
}
}
//Or lastly, no menu
}else if(statsCase == 3){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st1'){
myspan.style.display = 'none';
}
if(myspan.id == 'st2'){
myspan.style.display = 'none';
}
}
document.getElementById('st3').src = 'images/expand.gif';
}
//Set the cookie with the username, state and set it to expire in one year.
SetCookie("statewin<?=urlencode($Username)?>",statsCase,"<?=(gettime() + (60*60*24*7*52))?>");
}

//Same as the above function, but for my 2nd menu
function toggleDrugs(){
//alert(drCase);
drCase+=1;
if(drCase > 3){ drCase = 1; }
if(drCase == 1){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st4'){
myspan.style.display = displayType;
}
if(myspan.id == 'st5'){
myspan.style.display = displayType;
}
}
document.getElementById('st6').src = 'images/contract.gif';
}else if(drCase == 2){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st4'){
myspan.style.display = displayType;
}
if(myspan.id == 'st5'){
myspan.style.display = 'none';
}
}
}else if(drCase == 3){
dr = 0;
while(myspan = spans.item(dr++)){
if(myspan.id == 'st4'){
myspan.style.display = 'none';
}
if(myspan.id == 'st5'){
myspan.style.display = 'none';
}
}
document.getElementById('st6').src = 'images/expand.gif';
}
SetCookie("statedr<?=urlencode($Username)?>",drCase,"<?=(gettime() + (60*60*24*7*52))?>");
}
//Function to set the cookies. I grabbed this off a tutorial site after my own wouldn't work in IE. This one doesn't seem to work in IE either, though it did when I tested it on the tutorial site.
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires)) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}


Apologies for the brief commenting - I'm not one to comment my own code, so I just added that in right now.

Can anyone spot my mistake? As I said, this works 100% fine in FireFox. It's just IE, both the old version (non-tabbed, not sure what version num), and the new version (tabbed one) that it won't work in.

Thanks,
Wraith

EDIT: By the way, the PHP function "gettime()" is one of my own. It basically returns the current GMT time in timestamp format.