<h1>Make YouTube video title the iframe ID</h1>
This will work for any YouTube iframe embed. Useful when using youtube ga tracking codes on the website. Use your browser html inspect tool to view the id's on each of the two video iframes.
<br/>
<br/>
<br/>
<iframe src="https://www.youtube.com/embed/dPg_LPAgQSw"></iframe>
<br/>
<br/>
<iframe src="https://www.youtube.com/embed/6n0rdQ7GYKU"></iframe>
<br/>
<br/>By [email protected]
$("iframe[src*='youtube.com']").each(function () {
var current_vid = $(this);
var video_id = $(this).attr("src").replace("https:", "").replace("//www.youtube.com/embed/", "");
$(current_vid).addClass("youtube-iframe").wrap("<div class='gaplayer'></div>");
// perform json function to get video title
$.getJSON("http://gdata.youtube.com/feeds/api/videos?q=" + video_id + "&v=2&alt=jsonc", function (data) {
var video = data.data.items[0],
id = video.id,
title = (video.title).toLowerCase().replace(/[^a-zA-Z0-9]+/g, "-");
// makes video title the iframe ID
$(current_vid).attr('id', title);
});
// set iframe embed parameters for styling and js enabling
var embed_parameters = "?rel=0&showinfo=0&iv_load_policy=3&autohide=1&wmode=transparent&enablejsapi=1";
$(".gaplayer .youtube-iframe").attr("frameborder", 0).attr('src', function (i, currentAttribute) {
return currentAttribute + embed_parameters;
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.