Niru Niru - 3 years ago 222
HTML Question

Loading Twitch Player fails with Twitch not defined

I am geting an error

Uncaught ReferenceError: Twitch is not defined
when I am trying to run the html code below:

<div class="custom-box well" id="templateHolder">
<script src="https://player.twitch.tv/js/embed/v1.js"></script>
<div id="testDivId"></div>
<script type="text/javascript">
var options = {width:600,height:400,channel:"cookingForNoobs"};
var player = new Twitch.Player("testDivId", options);
player.setVolume(0.5);
</script>
</div>


When I run it in jsfiddle it works and does not complain about not finding
Twitch
. However, as part of my page it does. The page correctly loads except for this div. Also using
<iframe src="http://player.twitch.tv/?channel=cookingfornoobs&amp;muted=true" width="100%" height="800px"></iframe>
will work. I am not sure if there is something blocking the call for loading the Twitch player.

Is there something I can check for to make sure its being handled correctly?

As a note it occurs in Chrome as well as Firefox and I am able to see player.twitch.tv js/embed v1.js file under the sources tab.

Answer Source

When new Twitch.Player is interpretted your library might still be loading.

Try wrapping the twitch initialization within "DOMContentLoaded" or in window.onload = function(){} if you are using jquery then within $(document).ready(){}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download