Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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.
2 Solutions
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.

            <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)));


                  // 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.');

                  // 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);
            <!-- other intro page code -->
            <form name="myForm">
                  <input type="checkbox" name="myCheckbox" onclick="ChangeCookieState(this.checked)">
                  Check box to skip this page next time
            <!-- other intro page code -->

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

(chomp, chomp)
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).


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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now