jQuery/jPlayer/Cookie question

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 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.jsand 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!


 $(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"
  });
 });

Here is a version which should do what you want.



 $(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.

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…

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

HA! That is brilliant!