javascript - YouTube API - Firefox/IE return error "X is not a function" for any 'player.' request -


i've been pulling hair out trying simple youtube api integration working in ff/ie , have had no luck far.

it sounds either scope issue or call being made before player initialized i've tried suggests it's not 1 of 2 things. of note, works in (only) chrome.

// async api load per yt documentation... var tag = document.createelement('script'); tag.src = "//www.youtube.com/iframe_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag);  // declare player , set basic functions... var player  playvideo = function() { player.playvideo(); } stopvideo = function() { player.stopvideo(); }  // yt api ready function... function onyoutubeiframeapiready() {   player = new yt.player('player', {     videoid: "tkjcg4bmays",     events: {       'onstatechange': onplayerstatechange     }   });    // after player object created, bind popup function page anchors...   var popup = $('#video-popup')   ,   popupframe = $(popup).children('.center')   ;    $('a[data-vid]').click(function(e){     e.preventdefault ? e.preventdefault() : e.returnvalue = false;      var clicked = $(e.target).closest('a')     ,   videoid = $(clicked).attr('data-vid')     ;      if (!$(popup).hasclass('working')){       // if popup isn't working, cue video , animate popup in...        player.cuevideobyid({videoid:videoid});        $(popup).addclass('working');       $(popup).css('display','block').animate({         opacity: '1'       }, 200, function(){         playvideo();         $(popup).removeclass('working');       });     }   }); } 

link test page api , popup code, working in chrome not in other browsers. http://www.crackin.com/dev/regions/pathbuild/

after imbedding half of keyboard keys forehead, stumbled across frustratingly simple root of problem... it's nothing visibility issue! arrgg!!! apparently, firefox , ie not have access player object if initialized while set display:none;, chrome smart enough still initialize , target hidden.

the fix, in particular case, load popup display:block opacity:0, initialize api, set popup display:none inside yt api init function. this:

first, css...

#video-popup { display: block; opacity: 0; } 

.. javascript...

var player ,   popup = $('#video-popup') ;  function onyoutubeiframeapiready() {     player = new yt.player('player', {         videoid: "tkjcg4bmays"     });     $(popup).css('display','none'); } 

now need go drunk.


additional stuff: after figuring out root of problem, able isolate additional ie browser problems , work out solutions. these in case else having similar issue, may able carve out own solution this. solution might bit situation-specific, if else putting youtube videos in popups , needs ie support, might help.

the fix above (setting display:none in yt ready function) didn't seem work ie8, fixed wrapping in brief timeout:

function onyoutubeiframeapiready() {     player = new yt.player('player', {         videoid: "tkjcg4bmays"     });     settimeout(function(){         $(popup).css('display','none');     }, 500); } 

ie7 bit trickier, seems if player ever hidden using display:none, becomes unavailable regardless of whether it's been initialized or not.

using conditional comments apply classes tag, can add ie specific css keep popup "visible" hide offscreen instead.

#video-popup { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: none; opacity: 0; } .ie7detect #video-popup { display: block; left: -101% } #video-popup.open { left:0 !important; } 

there issue in both ie7 , 8 youtube player still visible when parent container set opacity 0 (which happens while page still loading). tried adding rules further target contents of popup , set opacity also, didn't work , wasn't super clean either. settled on using loading class.

so popup has loading class:

<div id="video-popup" class="loading">     <!-- bunch of stuff in here --> </div> 

and function updated...

function onyoutubeiframeapiready() {     player = new yt.player('player', {         videoid: "tkjcg4bmays",     });     settimeout(function(){         $(popup).removeclass('loading');     }, 500); } 

css looks this...

#video-popup { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: none; opacity: 0; } .ie7detect #video-popup, #video-popup.loading { display: block; left: -100% } #video-popup.open { left:0 !important; } 

the last piece of puzzle incorporate classname 'open' popup function when it's suppose visible.

hope helps avoid potential headaches in future. on downside, may not quite enthusiastic drunk after addressing these issues might have otherwise been.

peace, yo.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -