Advertisement
johnmahugu

javascript - you tube video embed

Jun 28th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var YtIframe = (function() {
  2. "use strict";
  3.     var YtIframe = function (config) {
  4.         config.https  = config.https  || false;
  5.         config.width  = config.width  || 560;
  6.         config.height = config.height || 315;
  7.         config.cookie = (config.cookie === false) ? false : true;
  8.         // build src attribute for iframe, create iframe element and populate it with attributes regarding the config options
  9.         var videoSource = (false === config.https) ? 'http://' : 'https://';
  10.         videoSource    += (false === config.cookie) ? 'www.youtube-nocookie.com/embed/' : 'www.youtube.com/embed/';
  11.         videoSource    += (-1 === config.videoHash.indexOf('youtube')) ? config.videoHash : config.videoHash.split('v=')[1].split('&')[0];
  12.         var iframe = document.createElement('iframe');
  13.         iframe.setAttribute('width', parseInt(config.width, 10));
  14.         iframe.setAttribute('height', parseInt(config.height, 10));
  15.         iframe.setAttribute('src', videoSource);
  16.         iframe.setAttribute('frameBorder', 0);
  17.         iframe.setAttribute('allowFullScreen', '');
  18.         return iframe;
  19.     };
  20.     return function (config) {
  21.         // check if str or obj given, set default values if values are missing
  22.         if ('undefined' == typeof config)
  23.             return false;
  24.         if ('string' == typeof config)
  25.             config  = {videoHash: config};
  26.         if ('object' == typeof config && !config.videoHash)
  27.             return false;
  28.         return new YtIframe(config);
  29.     };
  30. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement