Forum Moderators: open

Message Too Old, No Replies

Reading cookies as strings and variables

         

ocon

3:54 am on Aug 12, 2011 (gmt 0)

10+ Year Member Top Contributors Of The Month



Right now a portion of my script looks like this:

layer.setMap("custom");


But I can also set it as something like:

layer.setMap(google.maps.MapTypeId.SATELLITE);


While both of these work, what I want to do is to make this value dynamic, based on the user's cookie, and to set a default to "custom":

function getCookie(name){
var cookies = document.cookie;
if (cookies.indexOf(name) != -1){
var startpos = cookies.indexOf(name)+name.length+1;
var endpos = cookies.indexOf(";",startpos)-1;
if (endpos == -2) endpos = cookies.length;
return unescape(cookies.substring(startpos,endpos));}
else return "";}

var defaultmap = (getCookie("defaultmap") == null) ? "custom" : getCookie("defaultmap");

layer.setMap(defaultmap);


Unfortunately this doesn't work. Custom works fine, but I think google.maps.MapTypeId.SATELLITE is coming in as "google.maps.MapTypeId.SATELLITE" and being read as a string and not a variable. How can I make it so "custom" will read as a string, and google.maps.MapTypeId.SATELLITE will read as a variable, while also not hard coding these names into the script?

I'm also concerned about when the variables are defined with the cookies, how do I make the variables not return an error if they haven't been defined yet.

lucy24

9:52 pm on Aug 13, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



:: looking vaguely around for someone who speaks better Javascript than I do* ::

Can you give a little more context? See, I keep wanting to say that your code thinks it's a string because you've told it to expect a string, but I don't think it's really that straightforward.

how do I make the variables not return an error if they haven't been defined yet

Your basic structure in all situations like this is
IF (variable)
THEN (do stuff)
ELSE (do other stuff that will bring the variable into existence, or skip the whole thing depending on the nature and purpose of the variable)

The first IF should also include a quick check that the variable is within its permitted range, to allow for "I'll just make something up" user-agents. For example, "is NaN" when it's supposed to be a number. (I've met this in real life.)


* A group that includes most of the known universe.

astupidname

8:39 am on Aug 14, 2011 (gmt 0)

10+ Year Member



but I think google.maps.MapTypeId.SATELLITE is coming in as "google.maps.MapTypeId.SATELLITE" and being read as a string and not a variable


I'm sure you are correct there. What I would do first of all is do not store the cookie with the whole google.maps.MapTypeId.SATELLITE path, all you really need store is the SATELLITE part (or of course ROADMAP or whichever of the MapTypeId's the user selected) as a string. Then when you retrieve the users defaultmap cookie, check for the presence of a property with the same name within google.maps.MapTypeId
So, then you would change this:
var defaultmap = (getCookie("defaultmap") == null) ? "custom" : getCookie("defaultmap");


To this:
var defaultMap = getCookie('defaultmap');
//in the event getCookie happened to actually return a value,
//but is not what we expected (tampered with or something),
//or did not return a useable value
if (!defaultMap || !google.maps.MapTypeId.hasOwnProperty(defaultMap)) {
layer.setMap('custom');
} else { //a google.maps.MapTypeId property name was found by getCookie
layer.setMap(google.maps.MapTypeId[defaultMap]); //using string property name access[]
}