I need to pass the value from one function to another in JavaScript but when I am trying to do this it is not working.
The problem is with floaded2() function to get the value of the theYoutubeGlobalState.
HTML:
<html>
<head>
<script src = "http://www.youtube.com/player_api"></script>
<script src = "jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="style.css" type="text/css" media="screen" charset="utf-8">
</head>
<body dir = "rtl">
<iframe src="http://www.youtube.com/embed/-cSFPIwMEq4" onload="floaded2()" title="YouTube video player" id="player" allowfullscreen="" frameborder="0" height="400" width="500"></iframe>
</body>
</html>
JavaScript:
var theYoutubeGlobalState;
//onYouTubePlayerAPIReady
function floaded() {
player = new YT.Player('player', {
videoId: '-cSFPIwMEq4',
events: {
'onStateChange': function (event) {
theYoutubeGlobalState=event.data; }
}
});
}
function floaded2() {
switch ( theYoutubeGlobalState )
{
case 0:
alert("Ended");
break;
case 1:
alert("Playing");
break;
case 2:
alert("Paused");
break;
}
}
You can check it: http://jsfiddle.net/3GGHW/1/. Could you please tell me what’s the problem.
I made it work here:
http://jsfiddle.net/3GGHW/2/
You set the wrong function to the onload event handler in the HTML — needs to be
floaded()notfloaded2()— and you needed to callfloaded2()fromfloaded(). That’s it.