Skip to content

Instantly share code, notes, and snippets.

@grobertson
Created May 19, 2015 18:49
Show Gist options
  • Save grobertson/d1c864a453a436594c26 to your computer and use it in GitHub Desktop.
Save grobertson/d1c864a453a436594c26 to your computer and use it in GitHub Desktop.
$('#id_video_id').live('paste', function() {
var onFail = function(found) {
alert('Unsupported video service or invalid URL');
console.log(found);
$(element).val('');
return false;
}
var element = this;
var parser = document.createElement('a');
setTimeout(function () {
parser.href = $(element).val();
var videoId = new String();
if (parser.hostname === "youtu.be") {
videoId = parser.pathname.replace('/', '');
$(element).val(videoId);
}else if(parser.hostname === "youtube.com" || parser.hostname === "www.youtube.com" ) {
var matches = parser.search.match(/v\=([A-Za-z0-9\-]+)/);
if (matches[1]) {
videoId = matches[1];
$(element).val(videoId);
} else {
onFail(parser.href);
}
} else {
onFail(parser.href);
}
}, 300);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment