0

I have a video I want to autoplay when a user lands on the home page. If the user clicks back to home, I don't want the video to autoplay.

I have set this up with the following script, but this is not working.

<script type="text/javascript" language="Javascript">// 
    function played(){
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf("played=") == 0) return 1;
    }
    var date = new Date();
    var days = 7;
    date.setTime(date.getTime()+(days*24*60*60*1000));
    document.cookie = "played=1"+"; expires="+date.toGMTString()+"; path=/";
    return 0;
    }
        if(played()==0){
        document.write("<video controls poster=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" class=\"fillWidth\"><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.mp4.mp4\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.webmhd.webm\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.oggtheora.ogv\" /><img src=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" title=\"Your browser does not support the video tag\"></video>");
    }
    else
    {
        document.write("<video controls autoplay poster=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" class=\"fillWidth\"><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.mp4.mp4\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.webmhd.webm\" /><source src=\"https://s3-ap-southeast-2.amazonaws.com/acceptdifference/30sec_14_07_Youtube_ClosedCaptions.oggtheora.ogv\" /><img src=\"http://acceptdifference.org.au/wp-content/uploads/2016/07/Accept-difference-fallback.jpg\" title=\"Your browser does not support the video tag\"></video>");
    }
</script>
  • I recommend you setup the video on every request, use local storage rather than cookies to track whether it's been played, and if it hasn't been played, simply call myVideo.play(). Much simpler. – Brian FitzGerald Jul 21 '16 at 02:17
  • See http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/ – guest271314 Jul 21 '16 at 02:18

1 Answers1

0
function played(){
for(){
return 1; //does not work
}
return 0;
}

Create a variable and return it

function played(){
a=0;
for(){
a=1; //does work
}
return a;
}
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151