0

I'm trying to set a cookie in a users session, relative to a path different than where the user currently is. (That is, the path I want the cookie to be relative to is "/", where the user is currently in in "/_CGI". I'm trying the following, but it's not working.

<script type="text/javascript">
    $(document).ready(setMobileBrowsingCookie());

    function setMobileBrowsingCookie()
    {
        document.cookie = "WF_BROWSING_MODE=MOBILE; path=/";
    }
</script>

From my Chrome developer console, I can see the cookie being set when I remove the optional path=/ arg, but if I remove it, the cookie will be set relative to the CGI handle /_CGI. Is the path arg not being used correctly?

1 Answer 1

2

You need to set the cookie expiration as well.

document.cookie = 'WF_BROWSING_MODE=MOBILE; expires=Fri, 30 Aug 2012 20:47:11 UTC; path=/'
Sign up to request clarification or add additional context in comments.

5 Comments

Got it, I'll try this now. What if I'm not interested in the expiration of the cookie? Is it possible to ignore this optional arg and only pass in the path arg?
I would always specify the date such that the cookie knows when it is to expire. Different browsers may behave differently without a expires value being explicitly defined (with most just expiring to end of session I believe).
Sorry Mike, I didn't mean to mark the question as resolved. The cookie doesn't seem to work when I add the path=/ arg to the cookie declaration. Right now, I'm calling it like this: document.cookie = 'WF_BROWSING_MODE=MOBILE; expires=' + cookie_date + '; path=/'; and it doesn't work.
@Sal So what was did you do to finally make it work (so I can update my answer if needed)?
Well, I'm actually trying to override an existing cookie. For some reason, browsers don't want to override the existing cookies, so when I passed the path=/ arg, it would ignore it. When I didn't pass the path arg, it would set the path relative to the user's current state. You're answer made me realize this, so I'll just ask another question about properly overriding an existing cookie. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.