JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old February 3rd, 2004, 11:56 AM
wendall-san wendall-san is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 wendall-san User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old February 3rd, 2004, 05:57 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 11 sec
Reputation Power: 8
Send a message via ICQ to stumpy Send a message via MSN to stumpy
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.
__________________
DevArticles Moderator
BlueSix - Web Development and Consulting

Reply With Quote
  #3  
Old February 9th, 2004, 01:20 PM
wendall-san wendall-san is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 2 wendall-san User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #4  
Old February 9th, 2004, 04:56 PM
stumpy's Avatar
stumpy stumpy is offline
May contain nuts.
Dev Articles Regular (2000 - 2499 posts)
 
Join Date: Aug 2002
Location: Sydney, AU
Posts: 2,058 stumpy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 6 m 11 sec
Reputation Power: 8
Send a message via ICQ to stumpy Send a message via MSN to stumpy
Try testing for "undefined" - in quotes, because it's string.

Reply With Quote
  #5  
Old February 18th, 2004, 07:16 PM
wendallsan wendallsan is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 4 wendallsan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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!

Reply With Quote
  #6  
Old April 19th, 2004, 06:25 AM
TEKSOS TEKSOS is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 TEKSOS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > trouble comparing cookies and queryStrings


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway