不将变量传递给另一个函数

时间:2012-08-03 10:00:43

标签: javascript html css

我需要在JavaScript中将值从一个函数传递给另一个函数,但是当我尝试执行此操作时,它无法正常工作。 问题在于使用floaded2()函数来获取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;            
            }
        }

您可以查看它:http://jsfiddle.net/3GGHW/1/。你能告诉我这是什么问题吗?

2 个答案:

答案 0 :(得分:0)

您似乎根本没有调用floaded()。将来,请尝试将[简化]测试用例直接粘贴到您的问题中。

答案 1 :(得分:0)

我在这里工作了:

http://jsfiddle.net/3GGHW/2/

您将错误的函数设置为HTML中的onload事件处理程序 - 需要floaded()而不是floaded2() - 您需要从floaded2()调用floaded() 。就是这样。