|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
trouble comparing cookies and queryStrings
Hi all, I'm having a hard time getting some variable checking to work. What I want to happen is check to see if a session var and a queryString var exists. If they do, compare them to see if they are the same. If they are not the same, or if there is no session var, replace the session var with the queryString var. The problem is that I seem to get a variety of responses from my tests to check the existance of these vars so that I can't easily compare them.
Here is my code: function changeHazardSessionVar(){ // test existance of vars cookieExists = Request.Cookies("hazard"); queryExists = Request.QueryString("hazard"); write("cookieExists = " + cookieExists); write("queryExists = " + queryExists); // if query var exists . . . if (queryExists){ queryVar = Request.QueryString("hazard"); write("query var = " + queryVar); // if session var exists . . . if (cookieExists){ cookieVar = Request.Cookies("hazard"); write("cookie var = " + cookieVar); // compare query var and session var, if not the same . . . if (queryVar != cookieVar){ write("query var is not the same as cookie var."); // assign session var to value of query var Response.Cookies("hazard") = queryVar; } // if session var does not exist . . . } else { write("cookie var does not exist."); // assign session var to value of query var Response.Cookies("hazard") = queryVar; } } write("final value of cookie var = " + Request.Cookies("hazard")); } When this code is run with the queryString var "hazard" set to 1, this is the output: cookieExists = queryExists = 1 query var = 1 cookie var = query var is not the same as cookie var. final value of cookie var = 1 It seems that the 'cookieExists = Request.Cookies("hazard")' line of the code is not returning a TRUE / FALSE value. Also, it seems that although there is (presumably) no cookie set, the javascript is running a comparison between the value of the queryVar and the cookieVar, which I don't think it should do if there is no cookie. Can anyone help me with this? I just don't understand how I am getting non- true/false responses from my initial tests in the script, and how later the script acts like there is a value assigned to variables that I don't think should have anything assigned to them. thanks it advance! |
|
#2
|
||||
|
||||
|
Not exactly sure what you are trying to do, but as a general rule, I steer clear of trying to set and read cookies in the one page - I believe they were only designed to be set once per page, then read in another.
|
|
#3
|
|||
|
|||
|
hmm, let me try to make my question more simple:
I want to test for a querystring, and if it doesn't exist, I want to test for a cookie. My problem is that I can't figure out how to test these things because I can't get them to evaluate to true or false. When I test for a cookie and no result is found, the result of the test is "" (an empty string), when I would expect the result to be undefined or false. When I test for a querystring that doesn't exist, the result of the test is undefined. how do you test to see if a var is undefined? Undefined doesn't seem to equate to true or false, and I can't get the code "if (testMe == undefined) . . . " to work at all. also, how do you test to see if a var is equal to "" (an empty string)? Again, code such as 'if (testMe == "") . . . ' doesn't seem to work. thanks! |
|
#4
|
||||
|
||||
|
Try testing for "undefined" - in quotes, because it's string.
|
|
#5
|
|||
|
|||
|
OK, I have done some testing of this undefined thing with the querystring and the cookies collections. It seems that if a querystring item has not declared, it is returned as undefined. But a cookie item that hasn't been declared, it is returned as an empty string "", which can also be tested to equal the value 0. The thing that really has me confused is that I can't seem to test the querystring at all-- tests for the value 0, "undefined", and undefined (as a constant, not a string) all fail, even though when you go to write it out, it writes out as "undefined".
Check out this code: //NOTE: Request.QueryString("hazardID") and Request.Cookies("hazardID") have both NOT BEEN DECLARED. write("querystring hazardID = " + Request.QueryString("hazardID")); // returns "undefined" // test querystring against value 0 -- fails if (Request.QueryString("hazardID") == 0) write("querystring hazardID = 0"); else write("querstring hazardID != 0"); // test querystring against value "undefined" (string) and undefined (constant) -- both of these tests fail if(Request.QueryString("hazardID") == "undefined") write("querystring hazardID = undefined"); else write("querystring hazardID != undefined."); if(Request.QueryString("hazardID") == undefined) write("querystring hazardID = *undefined*"); else write("querystring hazardID != *undefined*"); // here is the part for the cookie collection write("cookie hazardID = " + Request.Cookies("hazardID")); // returns "" if (Request.Cookies("hazardID") == 0) write("cookie hazardID = 0"); -- this returns true!! else write("cookie hazardID != 0"); when run, the output of this code looks like this: querystring hazardID = undefined querstring hazardID != 0 querystring hazardID != undefined. querystring hazardID != *undefined* cookie hazardID = cookie hazardID = 0 At least now I understand this (apparent) inconsistancy in the scripting language. So, now, can anyone figure out how to test the existance of a var in the querystring collection??? I have everything else figured out at this point . . . thanks! |
|
#6
|
|||
|
|||
|
A solution to test if a QueryString key exists
// create your variable
Hashtable queryString = new Hashtable(); // iterate through keys and fill your Hashtable foreach(string key in Request.QueryString.Keys) { queryString[key] = Request.QueryString[key]; } // now you can test if queryString contains your key (bool returned) label1.Text = queryString.Contains("mode").ToString(); // finally, you can access the values string modeValue; if (queryString.Contains("mode")) { modeValue = queryString["mode"].ToString(); } else { //set default value modeValue = "view"; } I hope this helps. Terry TEKSOS.com |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > trouble comparing cookies and queryStrings |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|