Youtube iFrame Api示例 - 错误:“JSON未定义”?

时间:2011-03-14 10:11:22

标签: javascript api youtube

我正在尝试新的iFrame Api考试@ http://code.google.com/apis/youtube/iframe_api_reference.html(以下代码..)

Everythings在Chrome浏览器上有效,但在firefox 3.0和IE6上有错误报告 - > JSON未定义在线:13 播放器仍在工作,但javascript完全崩溃。

我试图在页面顶部获取json2.js库(http://www.json.org/)脚本,但没有什么比这更好了。 如何在这些旧浏览器上安排此JSON错误?

<html>
  <body>
    <!-- 1. The <div> tag will contain the <iframe> (and video player) -->
    <div id="player"></div>
    <script src="js/json2.js"></script>
    <script language="javascript">
      // 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() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          videoId: 'u1zgFlCw8Aw',
          playerVars: { 'autoplay': 1, 'controls': 0, 'showinfo': 0, 'loop': 1, 'rel' : 0, 'showsearch' : 0, 'enablejsapi' : 1},
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }

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


      function onPlayerStateChange(event) {
        //
      }

    </script>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

我也被这个咬了。 Youtube iframe API需要HTML5 postMessage功能,IE7和其他旧浏览器都没有。

Source

答案 1 :(得分:0)

您应该在JSON中使用双引号,因为实际上需要在双引号之间设置字符串。 在高度和宽度处删除单引号并只写整数,因为它不是字符串。

相关问题