Last active
December 11, 2015 20:38
-
-
Save karma86/4656523 to your computer and use it in GitHub Desktop.
Javascript file to redirect to a certain url when a youtube video ends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Place the <div id="player"></div> on your content page | |
| var tag = document.createElement('script'); | |
| // This is a protocol-relative URL as described here: | |
| // http://paulirish.com/2010/the-protocol-relative-url/ | |
| // If you're testing a local page accessed via a file:/// URL, please set tag.src to | |
| // "https://www.youtube.com/iframe_api" instead. | |
| tag.src = "//www.youtube.com/iframe_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 onYouTubeIframeAPIReady() { | |
| player = new YT.Player('player', { | |
| height: '390', | |
| width: '640', | |
| videoId: 'u1zgFlCw8Aw', | |
| events: { | |
| 'onReady': onPlayerReady, | |
| 'onStateChange': onPlayerStateChange | |
| } | |
| }); | |
| } | |
| // 4. The API will call this function when the video player is ready. | |
| function onPlayerReady(event) { | |
| event.target.playVideo(); | |
| } | |
| // 5. The API calls this function when the player's state changes. | |
| // The function indicates that when a video ends (state=0), | |
| // the page redirects to a certain url | |
| var done = false; | |
| function onPlayerStateChange(event) { | |
| if (event.data == YT.PlayerState.ENDED && !done) { | |
| window.location.replace("http://www.google.com"); | |
| done = true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment