Reputation: 374
Trying to change/modify the source of any/all iframe on the page where the src includes youtube.com, and get the video id from the src url.
Here's what I have so far that's not working.
$(document).ready(function(){
changeYoutube();
function changeYoutube()
{
$('iframe').each(function() {
var that = $(this);
var href = that.attr('src');
if(href.search('youtube.com') != -1) {
that.attr('href', href+'&autoplay=1');
}
});
});
I also need to put the video id from the url into a var. Example url: http://www.youtube.com/embed/hP9rBDYYFvw?fs=1&feature=oembed Where "hP9rBDYYFvw" is the video id in that url.
Thanks
Upvotes: 1
Views: 1217
Reputation: 82
This is the solution:
<script type="text/javascript">
function changeYoutube() {
$('iframe').each(function () {
var that = $(this);
var href = that.attr('src');
if (href.search('youtube.com') != -1) {
that.attr('src', href + '&autoplay=1');
// code to get id
var tmp = href.split("?");
var tmp2 = tmp[0].split("/");
var ID = tmp2[tmp2.length - 1];
}
});
}
$(document).ready(function () {
changeYoutube();
});
</script>
First You have set the href attribute but you must set src attribute of iframe. Second You must first define the function that You call after document ready, and in Your code you haven't close the function. Best Regards.
Upvotes: 1
Reputation: 1409
$(document).ready(function(){
changeYoutube();
});
function changeYoutube() {
$('iframe').each(function() {
if($(this).attr('src').search('youtube.com') != -1) {
var href = $(this).attr('src');
$(this).attr('src', href+'&autoplay=1');
}
});
}
it's just another style from yours. perhaps it works..please try
Upvotes: 0
Reputation: 18064
You are missing one closing brace }
.
Replace last line });
with }});
Upvotes: 0