Skip page on future visit with checkbox

I have browsed through the questions here and found some helpful but incomplete for what I need. I would like to write a value in a cookie with a checkbox. In my 'intro' page there will be a checkbox that a user can select to automatically forward the user to the main site when the checkbox is selected. How can I do this with JS? I can find the code to set a cookie and read it so that after the first or even x number of times visited will automatically foward to the next page but I would like the user to select it instead of automatically doing it after the first time.
ken_freyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gator4lifeCommented:
I think this is a good simple example of what you want to do.  The code below contains one checkbox that alternately sets and deletes a cookie depending on its checked property.  Also, the first thing the code does is check to see if the cookie has been set when the page starts to load.  If it finds the cookie that the checked checkbox set, then it automatically redirects a user to another URL (just change the 'nextpage.html' URL to what you want in the code).  The thing I like about this method is it doesn't require your users to submit a form.

<html>
      <head>
            <title>Intro Page</title>
            <script type="text/javascript">
            <!--
                  // try to retrieve the 'skipintro' cookie set by the checkbox on the intro page
                  var sSkipIntro = GetCookie('skipintro');

                  // if the cookie is found, immediately go to 'nextpage.html' and skip intro page
                  if (sSkipIntro) window.location.href = 'nextpage.html';

                  // function to set a cookie
                  function SetCookie(sName, sValue, dExpires, sPath, sDomain, bSecure) {

                        var oCookie = sName + '=' + escape(sValue) +
                                                ((dExpires) ? '; expires=' + dExpires.toGMTString() : '') +
                                                ((sPath) ? '; path=' + sPath : '') +
                                                ((sDomain) ? '; domain=' + sDomain : '') +
                                                ((bSecure) ? '; secure' : '');

                        document.cookie = oCookie;
                  }

                  // function to retrieve a cookie
                  function GetCookie(sName) {

                        with (document.cookie) {

                              if (length > 0) {

                                    var sExtendedName = sName + '=';
                                    var iExtendedNameBeg = indexOf(sExtendedName);

                                    if (iExtendedNameBeg != -1) {

                                          var iValueBeg = iExtendedNameBeg + sExtendedName.length;
                                          var iValueEnd = indexOf(';', iValueBeg);

                                          if (iValueEnd == -1) iValueEnd = length;

                                          return(unescape(substring(iValueBeg, iValueEnd)));
                                    }
                              }
                        }

                        return(null);
                  }

                  // function to delete a cookie
                  function DeleteCookie(sName, sPath, sDomain) {

                        if (GetCookie(sName)) {

                              document.cookie = sName + '=' +
                                                            ((sPath) ? '; path=' + sPath : '') +
                                                            ((sDomain) ? '; domain=' + sDomain : '') +
                                                            '; expires=Thu, 01-Jan-70 00:00:01 GMT';
                        }
                  }

                  // function to test if cookies are enabled
                  function CookiesEnabled() {

                        SetCookie('cookiesenabled', 'true');

                        if (!GetCookie('cookiesenabled')) {

                              window.alert('You must enable cookies to use this functionality.');
                              return(false);
                        }
                              return(true);
                  }

                  // function to toggle the 'skipintro' cookie set by the checkbox on the intro page
                  function ChangeCookieState(bChecked) {

                        if (CookiesEnabled()) {

                              // if checkbox is checked, create the 'skipintro' cookie, else delete it;
                              // this way, users can toggle the value the first time through the intro
                              // page before the cookie remains set
                              if (bChecked) {

                                    // set cookie to expire one year from now
                                    var dNow = new Date();
                                    dNow.setTime(dNow.getTime() + (365 * 24 * 60 * 60 * 1000));

                                    SetCookie('skipintro', 'true', dNow);
                              }
                              else
                                    DeleteCookie('skipintro');
                        }
                  }
            //-->
            </script>
      </head>
      <body>
            <!-- other intro page code -->
            <form name="myForm">
                  <input type="checkbox" name="myCheckbox" onclick="ChangeCookieState(this.checked)">
                  Check box to skip this page next time
            </form>
            <!-- other intro page code -->
      </body>
</html>

Let me know if you have any questions or need any further explanation.  Good luck!

gator4life
(chomp, chomp)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anderson22Commented:
I have created a test page for you.  You can simply view the source to get the code.  Some basic functions are used (contributed by webreference.com).

http://www.pebuk.com/examples/20561066/cookie.html

-rca
0
ken_freyAuthor Commented:
Thanks guys, looks like exactly what I needed. I'll try it a little later and get back with you. Again, thank you very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.