Skip to content

Instantly share code, notes, and snippets.

@chrisjhoughton
Created September 22, 2014 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjhoughton/b73f8396d9b6484f83f0 to your computer and use it in GitHub Desktop.
Save chrisjhoughton/b73f8396d9b6484f83f0 to your computer and use it in GitHub Desktop.
Get the video embed code from either a Vimeo or Youtube URL. Depends on Mout (moutjs.com)
var getParam = require("mout/queryString/getParam");
module.exports = function (url) {
if (!url) {
return "";
}
// Youtube
if (url.indexOf("youtube.com") !== -1) {
// Get the id
var videoId = getParam(url, "v");
if (videoId) {
return '<iframe width="620" height="400" src="//www.youtube.com/embed/'+videoId+'" frameborder="0" allowfullscreen></iframe>';
}
}
// Vimeo
else if (url.indexOf("vimeo.com") !== -1) {
// get the id
// e.g. "http://vimeo.com/87110435"
var arr = url.split(/http:\/\/vimeo.com\//)
var videoId = arr[1];
// insert to embed code
return '<iframe src="//player.vimeo.com/video/'+videoId+'" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment