SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    cookie trouble on ie7

    i've got a php, form, javascript/ajax thing. trouble with sessions/cookies not being set in ie7 always.

    session cookies are being set mainly with php's session() but there is one bit of js involved in dealing with setting cookies:

    Code:
    	var allcookies=document.cookie;
    	var session=allcookies.indexOf("session=");
    	if(session==-1){	//if no session cookie set
    		document.cookie="session="+newSessionID+"; path=/";
    		allcookies=document.cookie;
    	}
    that's in an external js file, which is generally working fine in all browsers including ie7. the newSessionID variable is coming from this in the html:

    Code:
    <script type="text/javascript">newSessionID="899087bc1e9ce2f5943cc3d49fd32fa2"; form_page="where-we-are";</script>
    <script type="text/javascript" src="includes/howfar.js"></script>
    (the howfar.js code is the file the first bit of code above is in.)

    so can anyone see anything wrong with that?

    also, i've asked this on the php list but i'll pop it on the end here as well (it's semi php, semi js question) (this is trying to find out the source of the same problem as the above question is trying to fix):

    on an ajax type of http request i'm setting a cookie using the php function session(). this works ok in other browsers but not ie7 (possibly other ie's). i found stated on various webpages (e.g. http://stackoverflow.com/questions/3...ernet-explorer ) that there's a problem with ajax and iframes and cookies. could this be related. to be honest i don't even know what an iframe is. is it an html element. if so i'm definitely not using an iframe.

    thanks.

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The value of a cookie is a string. Use parseInt() to convert the value of session to an integer before checking if it's equal to -1.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    surely that happens automatically, no? not if i were using === but with == surely there's a type conversion which happens?

  4. #4
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    anyway my problem which i can not fathom at the moment isn't to do with cookies not being set it seems. that's fine. it's something to do with the communication of cookies with requests or the code on the php server which deals with it. but seeing as it's a 100% fine in safari and firefox but intermittently working and not working in ie7 that indicates possibly that it's something to do with javascript because the browser doesn't make much difference to php.

  5. #5
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    surely that happens automatically, no? not if i were using === but with == surely there's a type conversion which happens?
    It may happen in some browsers, but JavaScript isn't supposed to do any automatic type casting on strings (that I'm aware of). PHP, on the other hand, will gladly convert just about anything you throw at it.

  6. #6
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, i shall change that as you sugest then. thanks.

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    i haven't been able to find the exact situation we're talking about described in my js book but i have found this which is very similar:


    "1" == true

    This expression evaluates to true ... The boolean value true is first converted to the number 1, and the comparison is done again. Next, the string "1" is converted to the number 1. Since both numbers are nw the same, the comparison returns true;
    actually, i've just realisd, this is all completely irrelevant! we're actually talking about the return value of indexOf which returns -1 when what was being searched for wasn't found. so i don't think i need parseInt()

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I hate to say this but when using the == operator, JavaScript performs type-casting on strings when comparing strings with numbers.

    This is why using the === operator will always provide you with more accurate results.

    Using the Equality Operators (the == operator)

    When type conversion is needed, JavaScript converts String, Number, Boolean, or Object operands as follows.

    • When comparing a number and a string, the string is converted to a number value. JavaScript attempts to convert the string numeric literal to a Number type value. First, a mathematical value is derived from the string numeric literal. Next, this value is rounded to nearest Number type value.
    • If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false.
      If an object is compared with a number or string, JavaScript attempts to return the default value for the object. Operators attempt to convert the object to a primitive value, a String or Number value, using the valueOf and toString methods of the objects.
    • If this attempt to convert the object fails, a runtime error is generated.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •