Last Updated: February 25, 2016
·
2.555K
· elclanrs

Embed Youtube video from link with custom params

$.fn.linkToYoutube = function(ops) {

  var o = $.extend({
    width: 480,
    height: 320,
    params: ''
  }, ops)

  return this.each(function () { 
    var $link = $(this),
        href = $link.attr('href'),
        id = /\?v\=(\w+)/.exec(href)[1],
        iframe = '<iframe style="display: block;"'+
        ' class="youtube-video" type="text/html"'+
        ' width="' + o.width + '" height="' + o.height +
        ' "src="http://www.youtube.com/embed/' + id + '?' + o.params +
        '&amp;wmode=transparent" frameborder="0" />'
    $link.replaceWith(iframe) 
  })

}

Demo: http://jsfiddle.net/elclanrs/6ZS3w/

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside