YouTube URL Parameters

We want this to be the best online community of Aggie fans in the world. If you know of something that will make it better, let us know.
User avatar
CodingAggieBlue
Posts: 262
Joined: November 3rd, 2010, 6:25 am
Location: Logan, UT
Has thanked: 311 times
Been thanked: 23 times

YouTube URL Parameters

Post by CodingAggieBlue » July 15th, 2014, 10:16 pm

I was hoping to embed the following video in response to madmaxvol's post about the cfb trophy.
https://www.youtube.com/v/Yzht2_41caU?start=237&end=255

However when I went to preview my post, nothing showed up. I modified my URL as follows: https://www.youtube.com/v/Yzht2_41caU?v ... 37&end=255. This tricks the inserted Javascript code into embedding my URL. However, it still strips off the other URL parameters.

I wanted to use the YouTube URL parameters described here. Specifically I wanted to use the start and end parameters to limit which section of the video would play. I believe that this can be done by modifying the code as follows:

Code: Select all

Disabled language="Javascript">
	var allowedparams = ['start', 'end'];
	var usedparams = '';
	var a = 'https://www.youtube.com/v/Yzht2_41caU?v=Yzht2_41caU&start=237&end=255';
	var temp = new Array();
	temp = a.split('?');
	variablestring = temp[1];
	variables = variablestring.split('&');
	for(var i in variables){
		variable = variables[i].split('=');
		if(variable[0] == "v"){
			videoId = variable[1];
			break;
		} else if (allowedtags.indexOf(variable[0]) > -1) {
			usedparams.concat('&' + variables[i]);
		}
	}
	document.write('<iframe style="z-index:1;" title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/' + videoId + '?wmode=transparent' + usedparams + '"' + ' frameborder="0" wmode="Opaque" allowfullscreen></iframe><br /><a href="' + a + '" class="postlink-local" target="_blank">Watch on YouTube</a>');
</script>
The result is the following URL: http://www.youtube.com/embed/Yzht2_41ca ... 37&end=255. This adds the functionality I'm looking for, but doesn't drastically change how things are currently working.

I recognize that not all YouTube parameters are desirable (such as autoplay). You can limit what parameters are accepted using the allowedparams array.

Note, I haven't actually tried to run the code above so it might not work as-is. You might also need to do some additional sanitizing of variablestring to prevent code injection. However, the general idea should work. Any thoughts?



User avatar
treesap32
Moderator
Posts: 16777
Joined: July 28th, 2005, 1:00 am
Location: Washington D.C.
Has thanked: 1112 times
Been thanked: 2655 times
Contact:

Re: YouTube URL Parameters

Post by treesap32 » July 16th, 2014, 7:53 am

I would love to be able to control start and end time in our embedded youtube vids.



Post Reply Previous topicNext topic