Reputation: 31
I use URL's like: http://example.com/videos/GF60Iuh643I I'm trying to use javascript inside iframe to embed youtube videos:
<iframe width="560" height="315" src="https://www.youtube.com/embed/<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.write(last_segment);
</script>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
It should give src="https://www.youtube.com/embed/GF60Iuh643I"
But instead the javascript remains unexecuted, any idea how to make this work?
Thank you!
Upvotes: 2
Views: 4255
Reputation: 3764
Try this(not tested). You get the iframe by id and then set the src to equal the link + the last segment.
<iframe id="videoframe" width="560" height="315" src="" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<script type="text/javascript">
var segment_str = window.location.pathname;
var segment_array = segment_str.split( '/' );
var last_segment = segment_array.pop();
document.getElementById('videoframe').src = 'https://www.youtube.com/embed/' + last_segment;
</script>
Upvotes: 2