I found this gem of a code snippet today, courtesy of http://www.geekstogo.com/forum/topic/268961-video-autoplay/
I was building a website out that I wanted to autoplay a YouTube video, but I didn’t want the video to autoplay every single time the viewer hit the home page. I knew the answer was in setting a cookie, and here’s some tasty javascript that does just that.
[sourcecode language=”javascript”]<script type="text/javascript" language="Javascript">// <![CDATA[
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("<embed src=\"http://www.youtube.com/v/z435EriT9eY&hl&autoplay=1\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"460\" height=\"340\"></embed>");
}
else{
document.write("<embed src=\"http://www.youtube.com/v/z435EriT9eY&hl\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"460\" height=\"340\"></embed>");
}
// ]]></script>[/sourcecode]
How this works, basically, is that the javascript sets a cookie. If there is no cookie, then it writes the embed code for the YouTube video with the autoplay flag set. If there IS a cookie, then it writes out the embed code with no autoflag in the parameters.
You can set the expiration of the cookie by changing the “var days = 1;” to something else, like 5 days or 7 days. I prefer to set it for just one day.
A big thanks to the person in the geekstogo forum who posted this solution. This really made my day!
That worked great, thanks!
thanx for this Amazing work 🙂
Thank you for the brilliant solution. This worked when pasted in HTML mode into a WordPress page.
This is a great solution. I am having trouble getting it to work with the Safari browser. Anyone have thoughts to solve this?
wow it works perfectly..amazingly ….i didnt think it was possible..but i made a client so happy with this
Do you have a fix for using FancyBox 2 auto playing a video on page load?
Really useful.
I used it as background voice intro for a website.
Width / Height both 0px
Thanks a Zillion!