Adding "once a day" cookie logic via Javascript
-
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?
- The topic ‘Adding "once a day" cookie logic via Javascript’ is closed to new replies.