Skip to content

Instantly share code, notes, and snippets.

@mediamichael
Created September 17, 2013 22:21
Show Gist options
  • Save mediamichael/6601484 to your computer and use it in GitHub Desktop.
Save mediamichael/6601484 to your computer and use it in GitHub Desktop.
Confirm if string is a YouTube URL, strip the youtube ID, and convert the url to a specific format and security.
var youTubeID;
var videoURL = "http://youtube.com/v/youtubeid"
if ( isYoutubeFile( videoURL ) == true ) {
//do something
}
function isYoutubeFile(vidURL) {
//console.log( "javascript: isYoutubeFile: vidURL: " +vidURL );
var videoURL;
if ( vidURL.toLowerCase().indexOf( "http://www.youtube.com/v/" ) == 0 ) {
youTubeID = vidURL.substr( 25, 11 );
if ( "https:" == document.location.protocol ) {
videoURL = "https://www.youtube.com/v/" + youTubeID;
} else {
videoURL = "http://www.youtube.com/v/" + youTubeID;
}
return true;
}
if ( vidURL.toLowerCase().indexOf( "http://www.youtube.com/watch?v=" ) == 0 ) {
youTubeID = vidURL.substr( 31, 11 );
if ( "https:" == document.location.protocol ) {
videoURL = "https://www.youtube.com/v/" + youTubeID;
} else {
videoURL = "http://www.youtube.com/v/" + youTubeID;
}
return true;
}
if ( vidURL.toLowerCase().indexOf( "https://www.youtube.com/v/" ) == 0 ) {
youTubeID = vidURL.substr( 26, 11 );
if ( "https:" == document.location.protocol ) {
videoURL = "https://www.youtube.com/v/" + youTubeID;
} else {
videoURL = "http://www.youtube.com/v/" + youTubeID;
}
return true;
}
if ( vidURL.toLowerCase().indexOf( "https://www.youtube.com/watch?v=" ) == 0 ) {
youTubeID = vidURL.substr(32, 11);
if ( "https:" == document.location.protocol ) {
videoURL = "https://www.youtube.com/v/" + youTubeID;
} else {
videoURL = "http://www.youtube.com/v/" + youTubeID;
}
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment