homepage Welcome to WebmasterWorld Guest from 54.226.0.225
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
A question about scope / setting a parameter
scope, parameter
bramley




msg:4468603
 1:11 am on Jun 23, 2012 (gmt 0)

The issue is that vid is undefined at line videoId:vid

vid="hs83kjsgh3";

bplayer = new YT.Player('idd2', {
height: '480',
width: '853',
videoId: vid,
background:'#000000',
bgcolor:'#000000',
wmode:'window',
events: {
'onReady': bonPlayerReady,
'onStateChange': bonPlayerStateChange
}
});


Can anyone explain / say what I need to do here ?

Thanks !

 

Fotiman




msg:4468622
 3:37 am on Jun 23, 2012 (gmt 0)

The problem seems to be that hs83kjsgh3 is not a valid YouTube Video ID. For example, attempting to put that id directly in a YouTube URL also fails with:
"An error occurred during validation.
Sorry about that."

bramley




msg:4468668
 10:00 am on Jun 23, 2012 (gmt 0)

I made up the id - that's not the issue.

The code is part of a function - setupPlayer(vid)
but I don't know how to pass vid to the new YT.Player.

Although it looks to be in scope i get a 'vid is undefined' at that point.

Fotiman




msg:4468702
 2:41 pm on Jun 23, 2012 (gmt 0)

Here's an example from the YouTube API. I've modified it to declare the videoId as a global variable as you've done, and it works when a valid videoId is passed in. That's why I suspected the bogus videoId:


<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>

<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "http://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
//vid="hs83kjsgh3";
vid='u1zgFlCw8Aw';
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: vid,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}

// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>

bramley




msg:4472546
 5:00 pm on Jul 4, 2012 (gmt 0)

Thanks Fotiman.

It is working now.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved