WordPress.org

Ready to get started?Download WordPress

Forums

Adding "once a day" cookie logic via Javascript (3 posts)

  1. frontdesk
    Member
    Posted 2 years ago #

    My homepage uses the Yahoo Media Player to play videos, so I include the following on that page to tell YMP to automatically start playing any video links on that whenever that homepage is loaded:

    <script type="text/javascript">
        var YWPParams =
        {
            autoplay: true
        };
    }
    </script>
    
    <script type="text/javascript" src="http://webplayer.yahooapis.com/player.js"></script>

    This code to insert the "autoplay: true" parameter works very well.

    Of course, if you visit my homepage several times in one session then the videos will annoyingly automatically play each time, so I want to add some lines of Javascript that will use cookies and thus only insert the "autoplay: true" parameter once a day (the default is "autoplay: false").

    Here is the same code with the cookie logic inserted:

    <script type="text/javascript">
    
    var now = (new Date()).getTime();
    var lastTime = 0;
    var lastTimeStr = localStorage['lastTime'];
    if (lastTimeStr) lastTime = parseInt(lastTimeStr, 10);
    if (now - lastTime > 24*60*60*1000) {
        var YWPParams =
        {
            autoplay: true,
            volume: 0.5
        };
    }
    localStorage['lastTime'] = ""+now;‚Äč
    </script>
    
    <script type="text/javascript" src="http://webplayer.yahooapis.com/player.js"></script>

    This cookie code is not doing anything. It never seems to insert my "autoplay: true" parameter for YMP.

    I have tried several cookie scripts but the results are always the same (i.e., my videos never autostart - let alone autostart only once a day).

    What am I doing wrong?

  2. frontdesk
    Member
    Posted 2 years ago #

    Maybe I need to rephrase my question.

    Does anyone know how I can add

    <script type="text/javascript">
        var YWPParams =
        {
            autoplay: true
        };
    </script>
    <script type="text/javascript" src="http://webplayer.yahooapis.com/player.js"></script>

    ... to the bottom of my front page only once per session? (once per day would be OK also)

  3. frontdesk
    Member
    Posted 2 years ago #

    Problem solved. For the record I used a combination of a cookie script from owt4nowt.ca that I inserted into a child theme override of functions.php.

    Here is my child theme override of functions.php

    <?php
    
    /*************************** Add Yahoo Media Player to footer ***************************/
    
    function add_this_script_footer(){ ?>
    
    <script type="text/javascript">
    
    // begin cookie code
    // ++++++++++++++++++++++++++++++++++++++++++
    // Run Once Per Session
    //
    // Replace the alerts by functions that need to
    // be run once per session.
    //
    // Written by: Michael Regan
    // Website   : www.owt4nowt.ca
    //
    // Released under the GPL.
    // ++++++++++++++++++++++++++++++++++++++++++
    var key_value = "myTestCookie=true";
    var foundCookie = 0;   
    
    // Get all the cookies from this site and store in an array
    var cookieArray = document.cookie.split(';');   
    
        // Walk through the array
        for(var i=0;i < cookieArray.length;i++)
            {
                   var checkCookie = cookieArray[i];
            // Remove any leading spaces
                   while (checkCookie.charAt(0)==' ')
                   {
                     checkCookie = checkCookie.substring(1,checkCookie.length);
                   }   
    
            // Look for cookie set by key_value
                    if (checkCookie.indexOf(key_value) == 0)
                   {
                      alert("Found Cookie ");
                // The cookie was found so set the variable
                       foundCookie = 1;
                   }
        }
        // Check if a cookie has been found
        if ( foundCookie == 0)
        {
            // The key_value cookie was not found so set it now
            document.cookie = key_value;
            alert("Setting Cookie");
    //    }    move this to end
    // end cookie code
    
        var YWPParams =
        {
            autoplay: true,
            volume: 0.5
        };
    
        }   // moved from above
    </script>
    <script type="text/javascript" src="http://webplayer.yahooapis.com/player.js"></script>
    <?php } 
    
    add_action('wp_footer', 'add_this_script_footer', 20); ?>
    
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags