Created
May 19, 2015 18:49
-
-
Save grobertson/d1c864a453a436594c26 to your computer and use it in GitHub Desktop.
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
$('#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