0

I'm trying to manipulate an Youtube object via JavaScript. But once I call a function like seekTo(), JS engine tells me there is no such function:

<script type="text/javascript" src="swfobject.js"></script>    
<div id="ytapiplayer"></div>
<script type="text/javascript">
var params = { allowScriptAccess: "always" };
var atts = { id: "ytplayer" };
swfobject.embedSWF("http://www.youtube.com/v/tS3J2BkLamQ?enablejsapi=1&playerapiid=ytplayer", 
               "ytapiplayer", "425", "356", "8", null, null, params, atts);
</script>
<a href="javascript:void(0);" onclick="ytplayer.seekTo(10,true)">seek</a>

There are three IDs in the official example. I tried them all with no result.

Fiefox 6 AFAIK

UPD Finally I figured out such code must be run at the server side.

1
  • The reason all ids fail the same is because they don't matter. You get to choose any id you want with the atts parameter. Commented Jul 26, 2011 at 2:04

2 Answers 2

2

You will first need to make sure your ytplayer JS object is set to the right thing - you can do this inside a call to onYouTubePlayerReady():

function onYouTubePlayerReady(playerId) {
  // create a global variable called ytplayer
  ytplayer = document.getElementById("ytplayer"); 
}

I think the API calls this function for you once the player has loaded - you don't need to register it.

Edit: here is a link to a working jsfiddle:

http://jsfiddle.net/cZdZK/

Sign up to request clarification or add additional context in comments.

7 Comments

Variable 'ytplayer' is pre-available even without getElementById; it is a HTMLObjectElement. However I've placed an alert into that callback - it's not called for me: pastebin.com/w191azuT
Hm. Are there any errors on the js console? Is swfobject.js installed on your server? If you don't have it, you can link directly to ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js
I created a working example JS fiddle and updated the answer - let me know if you need any more help :)
There are no errors in the console. swfobject is placed in the same dir. Sorry, cannot find a free one-time HTML hosting.
Thank you Timothy, it works on JS fiddle! But not on my PC :) Looking at the JS fiddle code I've added onload() handler but still with no result. I'll investigate it further.
|
0

The answer is: the code should be run on the server side due to restrictions of Flash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.