Forum Moderators: open
I need a little help with cookies and onclick.
In IE the script I have works fine, I click on a menu tab, use set_cookie() and then reload the page to let the menu redraw itself for the new option. This is an option for people who wants different blog articles to appear at the top.
But, in FF, it doesn't work whatsoever. Won't set the darned cookie.
Here is my cookie code:
function get_cookie(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 set_cookie(name, value) {
var thisDate = new Date();
thisDate.setTime(thisDate.getTime()+30*24*60*60*1000); // one year
document.cookie = name + "=" + escape(value) + "; expires=" + thisDate.toGMTString();
}
... and here is how I set it ...
switch (get_cookie("teg")) {
case "life":
document.write( "<ul id='navlist'>" );
document.write( "<li><a href='' onclick='set_cookie(\"teg\",\"work\");return true;'>Work</a></li>" );
document.write( "<li id='active'><a href='' id='current'>Life</a></li>" );
document.write( "<li><a href='' onclick='set_cookie(\"teg\",\"musings\");return true;'>Musings</a></li>" );
document.write( "<li><a href='' onclick='set_cookie(\"teg\",\"politics\");return true;'>Politics</a></li>" );
document.write( "</ul>" );
break;
(other switch options left out)
Please help, it's been driving me mad for days now. Thanks!
Richard
I don't have time to pore over your code, so could you write your code to output the cookie string to the page with document.write, and then post the output here? Maybe the cookie string isn't in the right format, and that will be easier to see if we can see the cookie string itself. In my experience some browsers will make a cookie anyway even with a bad string and some won't.