• 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="https://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="https://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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter frontdesk

    (@frontdesk)

    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="https://webplayer.yahooapis.com/player.js"></script>

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

    Thread Starter frontdesk

    (@frontdesk)

    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="https://webplayer.yahooapis.com/player.js"></script>
    <?php } 
    
    add_action('wp_footer', 'add_this_script_footer', 20); ?>
    
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding "once a day" cookie logic via Javascript’ is closed to new replies.