Cookies and Redirects

I bet many of you have all worked with cookies once or twice in the past, but if you’re like me…you haven’t. I have a client that needs something like this to happen: “If you’ve been to my site, go to the home page. If you haven’t, go to the slide page.”

Since I have never “really” worked with cookies before, this was a little bit tricky. But I did find a workable solution. I am using JQuery’s cookie.js.

Add this code to your HEAD:

<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript">
$(function() {
    var COOKIE_NAME = 'cookie-page-name';
    $go = $.cookie(COOKIE_NAME);
    if ($go == null) {
        $.cookie(COOKIE_NAME, 'valueHere', { path: '/', expires: 7 });
        window.location = "ifCookieIsntSetGoHere.html"
    }
});
</script>

The only bits of code that need to be changed are: cookie-page-name, valueHere, and 7. From what I can tell, then names you choose to replace “cookie-page-name” & “valueHere” really don’t matter. The “7” is the number of days until this cookie expires.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.