Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:25:09+00:00 2026-06-11T07:25:09+00:00

How would I create a cookie that would store the randomly added body class

  • 0

How would I create a cookie that would store the randomly added body class for one browser session or for one day. My intention would be to randomly give every user a body background image and then store that image so that it won’t change every pagereload or when they go to page 2.

Site http://www.midnightlisteners.com/

i am using this jQuery plugIn: https://github.com/carhartl/jquery-cookie

but it does not work somehow

My jQuery code:

the code that I use:

if($.cookie('userBackground') === null) {
    var classes = ['body-bg1','body-bg2', 'body-bg3', 'body-bg4'];
    var randomnumber = Math.floor(Math.random()*classes.length);
    var chosenClass = classes[randomnumber];
    $('body').addClass(chosenClass );
    $.cookie('userBackground', chosenClass, { expires: 7, path: '/' });
} else {
   //todo verify cookie value is valid
   $('body').addClass($.cookie('userBackground'));
}

Errors i am getting:

Uncaught ReferenceError: require is not defined
Uncaught TypeError: Object function (a,b){return new e.fn.init(a,b,h)} has no method 'cookie'  

Are there other ways to do this? php? pure javascript?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:25:11+00:00Added an answer on June 11, 2026 at 7:25 am

    UPDATE:

    If you want to make it only last the length of the session then just use the session instead:

    <?php 
    if(!isset($_SESSION['bgclass'])) {
       // lets make our cookie!
       $classes = array('body-bg1','body-bg2', 'body-bg3', 'body-bg4');
    
       $classIndex = array_rand($classes);
    
       $_SESSION['bgclass'] = $classes[$classIndex];
    
    }       
    
    $bgclass = $_SESSION['bgclass'];
    ?>
    

    This way after the session times out or the browser is closed the user will get a new bgclass value.


    If you already have php running i would do it that way. Much better to handle this server side if you can. Its also a bit simpler:

    <?php 
    if(!isset($_COOKIE['bgclass'])) {
       // lets make our cookie!
       $classes = array('body-bg1','body-bg2', 'body-bg3', 'body-bg4');
       $expire = time()+(60*60*24); // expire 1 day form now
       $classIndex = array_rand($classes);
    
       $bgclass = $classes[$classIndex]; // had $class here as opposed to $classes
    
       setcookie('bgclass', $bgclass, $expire);
    
    } else {
      $bgclass = $_COOKIE['bgclass'];
    }
    ?>
    <html>
      <head></head>
      <body class="<?php echo $bgclass ?>">
        ...
      </body>
    </html>
    

    The key thing to remeber is that a cookie is essentially a response header so you have to do this before headers have been sent (ie. anything from php is output to the browser).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Versioning If your events changes you would create a new version of that event,
I have a class which would create a bean and the bean has few
I need to have script.sh , that would create files f1.txt and f2.txt with
How would I create a Mongoid model that has the ability to be saved
How would I create a DecimalFormat that would only have seven digits total and
How would I create a page that allows the user to view their profile.
How would I create the following scenario that rails can provide for carrierwave, using
How would I create an if statement within a cell that looks at another
I am using codeigniter's session class to handle my PHP sessions. One of the
I would like to create a page flow that allows the use to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.