Skip page on future visit with checkbox

Posted on 2003-03-24
Medium Priority
Last Modified: 2008-03-03
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.
Question by:ken_frey
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

gator4life earned 152 total points
ID: 8198922
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)

Assisted Solution

anderson22 earned 148 total points
ID: 8198962
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).



Author Comment

ID: 8199025
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question