# jQuery/jPlayer/Cookie question

**URL:** <https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446>\
**Category:** Factual Questions\
**Created:** [September 12, 2012, 12:22am UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446 "2012-09-12T00:22:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anamorphic](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/anamorphic/32/3057_2.png) [@Anamorphic](https://boards.straightdope.com/u/Anamorphic)\
**Post date:** [September 12, 2012, 12:22am UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446/1 "2012-09-12T00:22:53Z")

</div>

Any jQuery experts around?

I’m setting up a Shopify site for a friend of a friend. They have requested that the site have background music. Yes, yes, yes… I know. I know. I tried to talk them out of it, told them how people hate background music on websites, that it will drive off business, etc. No go. They’re stuck on the idea. It’s not my fault, I swear. Please stop looking at me like that.

Anyway, I have set up [jPlayer](http://www.jplayer.org/) on the site, with a simple icon to stop or start the music. Right now, the music is set to autoplay when the site loads. Clicking the icon stops the music.

Of course, when the user navigates to another page, the music starts right up again, and they need to hit stop again. What I want to do is set a cookie so if they ever hit ‘stop’, it won’t autoplay again, on return visits, or on new pages.

I downloaded [jquery.cookie.js](https://github.com/carhartl/jquery-cookie)and installed that. I have it set up so that when the user clicks on ‘stop’, it creates a cookie named autoPlay and sets it to “no”.

I have no idea what to do after that, though.

Here is the jPlayer code. What do I do to it to make it utilize the autoPlay cookie I set up? Any help would be very, very much appreciated!

```auto

 $(document).ready(function(){
  $("#jquery_jplayer_1").jPlayer({
   ready: function () {
    $(this).jPlayer("setMedia", {
     mp3: "mozart.mp3",
     oga: "mozart.ogg"
    }).jPlayer("play");
   },
   swfPath: "/js",
   supplied: "mp3, oga"
  });
 });

```

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [September 12, 2012, 1:53am UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446/2 "2012-09-12T01:53:12Z")

</div>

Here is a version which should do what you want.

```auto

 $(document).ready(function(){
  $("#jquery_jplayer_1").jPlayer({
   ready: function () {
     $(this).jPlayer("setMedia", {
     mp3: "mozart.mp3",
     oga: "mozart.ogg"
    });
    if ( $.cookie( "autoPlay" ) != "no" ) { 
      $(this).jPlayer("play");
    }
   },
   swfPath: "/js",
   supplied: "mp3, oga"
  });
 });

```

I don’t have an obnoxious jPlayer installation to test this on, however.

---

<div class="post-metadata">

**Author:** ![Anamorphic](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/anamorphic/32/3057_2.png) [@Anamorphic](https://boards.straightdope.com/u/Anamorphic)\
**Post date:** [September 12, 2012, 4:06am UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446/3 "2012-09-12T04:06:45Z")

</div>

**friedo** , I owe you a beer or other beverage of your choice. That worked! Thanks!

I still want to convince them to ditch the idea of this altogether, but at least this is a step…

---

<div class="post-metadata">

**Author:** ![fubbleskag](https://avatars.discourse-cdn.com/v4/letter/f/c2a13f/32.png) [@fubbleskag](https://boards.straightdope.com/u/fubbleskag)\
**Post date:** [September 12, 2012, 11:48am UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446/4 "2012-09-12T11:48:48Z")

</div>

find out their IP and have the player check for that and only autoplay when -they- visit 🙂

---

<div class="post-metadata">

**Author:** ![Anamorphic](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/anamorphic/32/3057_2.png) [@Anamorphic](https://boards.straightdope.com/u/Anamorphic)\
**Post date:** [September 12, 2012, 3:16pm UTC](https://boards.straightdope.com/t/jquery-jplayer-cookie-question/634446/5 "2012-09-12T15:16:31Z")

</div>

> [@fubbleskag](#):
>
> find out their IP and have the player check for that and only autoplay when -they- visit 🙂

HA! That is brilliant!
