SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Only play .mp3 intro once

    Hello,

    Wondering what is the best way to have a short intro .mp3 music file play only for the first hit to an index page. Subsequent hits from the same session and the file won't play again.

    I am guessing need to place a session cookie on the browser's computer and check for it, but how to integrate? Thx a lot. Brian.

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Just set a cookie that marks the visitor as having seen it. Only have the mp3 play if that cookie isn't present.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Stymiee

    Okay, but I forgot to say that I want it to play if they hit the page again later after re-opening their browser. So the cookie needs to know if this is the same surf session or new (to play the .mp3 again w/new). Any code examples (JS?) would further help me. Thanks everyone.

  4. #4
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Set the cookie to expire at the end of the session (which is the default behavior for cookies). Then every time they go to the page they won't hear it. But once they close their browser and then come back again they will hear it (and be sent another cookie that stops it for that session).

    The functions below are what you would use to set a cookie in JavaScript. To set the cookie use something like this:

    setCookie('nomusic', '1');

    and to check for it with JavaScript use something like this:

    if (!getCookie('nomusic'))
    {
    // do stuff that makes the music play
    }

    You might want to use Serverside programming for this as it is much more reliable then JavaScript but JavaScript should work well, too.

    Code:
    function setCookie(name, value, expires, path, domain, secure)
    {
        var today = new Date();
        today.setTime(today.getTime());
        if (expires)
        {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires = new Date(today.getTime() + (expires));
    
        document.cookie= name + "=" + escape(value) +
            ((expires) ? "; expires=" + expires.toUTCString() : "") +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            ((secure) ? "; secure" : "");
    }
    
    function getCookie(name)
    {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1)
        {
            begin = dc.indexOf(prefix);
            if (begin != 0) return null;
        }
        else
        {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
            end = dc.length;
        return unescape(dc.substring(begin + prefix.length, end));
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks again Sty - I will give it a shot.

    Cheers!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •