Reputation: 3
I have tried to apply the embed code from youtube url and everything work fine. But I want to find the solution if the string is not youtube link, then do not apply embed code.
Here is my code
$(document).ready(function() {
$('.youtube').html(function(i,v){
var id = v.split('watch?v=')[1]; // get the id so you can add to iframe
return '<iframe width="490" height="300" src="http://www.youtube.com/embed/' + id +"?wmode=opaque"+ '" frameborder="0" allowfullscreen></iframe>';
});
});
I realy don´t know how to do that ? Could anyone help me ?
Upvotes: 0
Views: 503
Reputation: 231
I would add a conditional looking for an index of 'watch?v=' before running your split function.
like this:
$(document).ready(function() {
$('.youtube').html(function(i,v){
// check for an instance of 'watch?v='
if (v.indexOf('watch?v=') !== -1) {
var id = v.split('watch?v=')[1]; // get the id so you can add to iframe
return '<iframe width="490" height="300" src="http://www.youtube.com/embed/' + id +"?wmode=opaque"+ '" frameborder="0" allowfullscreen></iframe>';
} else {
// return full url as string if not a youtube
return v;
}
});
});
Hope that helps!
Upvotes: 1