Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: JS and Cookies

  1. #1
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts

    JS and Cookies

    Hey Guys,

    I am working on a new project to do with cookies and then using the cookies on a refresh or on a load of a new page but for some reason when the page is reloaded it seems to create a new cookie with the same name it is called with the 2nd state like as if it is not checking for the previous one.

    I think I have tracked the problem down to this code which is run onload of page. If its not this I will put some more code up to try and work it out.

    Thanks guys

    Code:
    function loadCookieCheck()
    {
    var category=getCookie("showHide");
    
    	if (category == null)
      		{	setCookie('showHide','Hide');
    			category = 'Hide'; } 
    			j(this).showDiv();
    			
    	if (category == 'Show')
      		{ myFunction(); }
      		
      
    }
    .pLeAd InSaNiTy.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,915
    Thanks
    15
    Thanked 227 Times in 227 Posts
    javascript refers to a cookie as document.cookie, so your if statements have to be about document.cookie.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,620
    Thanks
    0
    Thanked 645 Times in 635 Posts
    Quote Originally Posted by syco__ View Post
    Code:
    function loadCookieCheck()
    {
    var category=getCookie("showHide");
    
    	if (category == null)
      		{	setCookie('showHide','Hide');
    			category = 'Hide'; } 
    			j(this).showDiv();
    			
    	if (category == 'Show')
      		{ myFunction(); }
      		
      
    }
    If that is the code you are using for cookie processing then the place to look further is inside the getCookie and setCookie functions that are elsewhere in your code. As DrDOS says, JavaScript uses document.cookie to reference cookies and so your functions will contain references to that object.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Regular Coder syco__'s Avatar
    Join Date
    Oct 2005
    Location
    Australia
    Posts
    194
    Thanks
    41
    Thanked 4 Times in 4 Posts
    Ok. I may missunderstand this but the cookie is setting correctly It is there but when I navigate to a new page and the cookie is set to Show it creates a new cookie with Hide in it. I find this is the only place in my code where the cookie is created and set to Hide but why is the code running if the cookie == show?
    .pLeAd InSaNiTy.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,340 Times in 4,306 Posts
    So show us your code for getCookie().

    And how do we now that the problem isn't in your myFunction() call??

    And you say it "seems" to create a second cookie. What evidence do you have of that?

    And what is this in this case? You are using j(this) but we don't know what either j or this is in this code.

    And not to ask a really silly question, but...What does any of this have to do with "DOM and JSON scripting"??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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