Coding a click

They have: 3 posts

Joined: Jan 2009

Hi and thank you for having me here.

I am currently designing a website and have a question regarding embedded video.

I have a Youtube video that I want autoplayed on my site, I have looked into the autoplay line and it's unsuitable as Youtube does not class an autoplayed video as viewed.

So the question would be can we fake a click on the play button so that it plays on loading and counts towards your stats?

Thanks and thanks again

He has: 629 posts

Joined: May 2007

Assuming you can initiate the "play" button with a script, you could take a leaf out of Sitepoint.com's web site. They have an "onclick" handler on the BODY element. A click anywhere on the page normally bubbles up - so they create a pop-up advert the first time a visitor clicks anywhere on the page. Perhaps you could adapt this to trigger the "play" button?

I don't know about the YouTube API so can't be more specific. Sorry.

Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

what about a Javascript like

<script type="text/javascript">
<!--
function clickit() {
document.getElementById('idofflash').click();
}
onload = clickit;
}
//-->
</script>

where "idofflash" is the id="someid" of the flash component you need to add

I am not sure this will click an embedded YouTube flash video, however, I do believe that a click anywhere on the YouTube video will play it, it is not necessary to target the play button

if this works, it has an advantage over webwiz's method because it will autoplay the video on document load (I believe that is your goal), not triggered by a click

[EDIT] I have been testing this, but the only way I can get the embedded YouTube video to play automatically is the autoplay=1 parameter which will not be counted because it was abused...

He has: 629 posts

Joined: May 2007

Okay. I just googled this. Check out this simple explanation:
http://www.loganrenz.com/wordpress/?p=334

Hope it helps.

They have: 3 posts

Joined: Jan 2009

webwiz wrote:
Okay. I just googled this. Check out this simple explanation:
http://www.loganrenz.com/wordpress/?p=334

Hope it helps.

Yes I am aware of both of these options Youtube gives you, but unfortunately Youtube does not count either od these actions as views.

I am currently playing around with that Javascript as I think it's the best chance I have. Many thanks to you both for the wise words

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

I think you are best off using the YouTube API including SWFObject

Be aware that you cannot test it on a local file, as there are security issues connecting your client computer to remote files - you need to upload your code somewhere to test it

If/when I come up with a solution, I'll post it back here

decibel.places's picture

He has: 1,494 posts

Joined: Jun 2008

ok, here we go, based on the demo here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
      YouTube Javascript API Autostart
    </title>
    <script src="swfobject.js" type="text/javascript"></script>

    <script type="text/javascript">
    setTimeout('myytplayer.playVideo()',5000);
    </script>
  </head>
  <body>
  <div>
    <div id="ytapiplayer">
      You need Flash player 8+ and JavaScript enabled to view this video.
    </div>
    <script type="text/javascript">
      // allowScriptAccess must be set to allow the Javascript from one domain to access the swf on the youtube domain
      var params = { allowScriptAccess: "always" };
     // this sets the id of the object or embed tag to 'myytplayer'. You then use this id to access the swf and make calls to the player's API
      var atts = { id: "myytplayer" };
      swfobject.embedSWF("http://www.youtube.com/v/OdqhDcAoQuc&amp;border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer",
                         "ytapiplayer", "425", "344", "8", null, null, params, atts);
    </script>

  </body>
</html>

You need to

1. Download SWFobject, not link to YouTube's

2. Change swfobject.embedSWF("http://www.youtube.com/v/OdqhDcAoQuc to the URL of your video

the setTimeout is very important, it will not work without it - but you may be able to set the timer to less than 5000 milliseconds

They have: 3 posts

Joined: Jan 2009

That's great Decibel, you have my gratitude and eternal respect

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.