slipvayne Posted August 22, 2010 Share Posted August 22, 2010 I'm pretty new at javascript... this is really the first code I'm trying to make using javascript and I've come to a little trouble. <script type="text/javascript"> var params = { allowScriptAccess: "always", bgcolor: "#FFFFFF"}; var atts = { id: "ytplayer" }; swfobject.embedSWF("http://www.youtube.com/v/<?=$embedid?>&enablejsapi=1&playerapiid=ytplayer&autoplay=1&color1=0x000000&color2=0xB90000", "myContent", "480", "360", "9.0.0", "expressInstall.swf"); ytplayer.addEventListener("onStateChange", "onPlayerStateChange"); function onPlayerStateChange(newState) { ytState=newState; if(ytState==0) window.location.reload(); } function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("ytplayer"); ytplayer.addEventListener("onStateChange", "onPlayerStateChange"); } </script> <div id="myContent"> <p>Alternative content</p> </div> Now, if ytState == 0, that means the video has ended play. I'm trying to test this out, by having the page reload when the video ends, using window.location.reload(); but it is not working. Any help or advice is appreciated. Quote Link to comment Share on other sites More sharing options...
Omirion Posted August 24, 2010 Share Posted August 24, 2010 is this the entire code? Quote Link to comment Share on other sites More sharing options...
Adam Posted August 25, 2010 Share Posted August 25, 2010 If you change the code to: if(ytState==0) { alert('foo'); window.location.reload(); } .. do you see the alert? Also do you see any errors in the error console? Quote Link to comment Share on other sites More sharing options...
slipvayne Posted August 29, 2010 Author Share Posted August 29, 2010 I changed it, and no, there was no alert. :/ Here is the link if you want to check it out: http://zwars.org/Vaduz/index.php?p=shuffle Could it be because my script is in between the body tags and not the head tags? I always read that it should be in the head tags but I've always put my javascripts in the body of my documents and never had any problems. Quote Link to comment Share on other sites More sharing options...
Adam Posted August 31, 2010 Share Posted August 31, 2010 When you go to the page there's a JavaScript error: Error: ytplayer is not defined Source File: http://zwars.org/Vaduz/index.php?p=shuffle Line: 222 Looking through your code I can't find anywhere it's defined either. Quote Link to comment Share on other sites More sharing options...
slipvayne Posted September 10, 2010 Author Share Posted September 10, 2010 I'm not sure how to define ytplayer. I have been using this as a guide: http://code.google.com/apis/youtube/js_api_reference.html Like I said, all I know about javascript is how to copy and paste. :/ Quote Link to comment Share on other sites More sharing options...
slipvayne Posted September 12, 2010 Author Share Posted September 12, 2010 I fixed my code, so that there is no error. But nothing happens, the page does not refresh when the video ends. :/ Here is what I currently have: <script type="text/javascript"> var params = { allowScriptAccess: "always", bgcolor: "#FFFFFF"}; var atts = { id: "myytplayer" }; swfobject.embedSWF("http://www.youtube.com/v/<?=$embedid?>&enablejsapi=1&playerapiid=myytplayer&autoplay=1&color1=0x000000&color2=0xB90000", "myContent", "480", "360", "9.0.0", "expressInstall.swf"); function onYouTubePlayerReady(playerId) { ytplayer = document.getElementById("myytplayer"); ytplayer.addEventListener("onStateChange", "onytplayerStateChange"); } function onytplayerStateChange(newState) { if (newstate = 0) { window.location.reload() } } </script> Although check the source on http://www.zwars.org/Vaduz/?p=shuffle because I may be changing the code around on there. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.