I am creating a website for a composer to include a page in a blog style with YouTube videos of concerts. I am attempting to block all the code for the videos from automatically loading on the page (and to warn of remote cookies) unless someone clicks/taps to watch the video. There is a custom field which contains the YouTube video ID, eg aBC_1deF_gH, and I created a new menu item type called 'Watch' in which I have added the following code to the watch_item.php file:
<div class="ytcover">Click to watch this video</div>
(It's not styled, yet)
At the end of the file I have added the following, which was based on this post: video lazy loading skipping first video in list
( function() {
var youtube = document.querySelectorAll( ".ytcover" );
for (var i = 0; i < youtube.length; i++) {
youtube[i].addEventListener( "click", function() {
var iframe = document.createElement( "iframe" );
iframe.setAttribute( "frameborder", "0" );
iframe.setAttribute( "allowfullscreen", "" );
iframe.setAttribute( "src", "https://www.youtube.com/embed/aBC_1deF_gH?rel=0&showinfo=0&autoplay=1" );
this.innerHTML = "";
this.appendChild( iframe );
} );
};
} )();
As you can see, the video code [not a real one here] is hard-coded into the JavaScript but I did this to test that the JavaScript works, which it does.
What I really want to have here is something like this:
iframe.setAttribute( "src", "https://www.youtube.com/embed/<?php echo $this->item->jcfields[1]->value; ?>?rel=0&showinfo=0&autoplay=1" );
Is this possible and, if so, how?
Edit - further tests:
I have also tried adding this as a shortcut:
$ytubeId = $this->item->jcfields[1]->value;
which does display correctly when echoed in a plain div.
So I modified the script to this:
( function() {
var youtube = document.querySelectorAll( ".ytcover" );
for (var i = 0; i < youtube.length; i++) {
var ytid = "https://www.youtube.com/embed/<?php echo $ytubeId; ?>?rel=0&showinfo=0&autoplay=1";
youtube[i].addEventListener( "click", function() {
var iframe = document.createElement( "iframe" );
iframe.setAttribute( "frameborder", "0" );
iframe.setAttribute( "allowfullscreen", "" );
iframe.setAttribute( "src", ytid );
this.innerHTML = "";
this.appendChild( iframe );
} );
};
} )();
But whilst the resulting script in the html is showing the correct URL, i.e.
var ytid = "https://www.youtube.com/embed/aBC_1deF_gH?rel=0&showinfo=0&autoplay=1";
The actual URL for the link is missing the video id, i.e:
src="https://www.youtube.com/embed/?rel=0&showinfo=0&autoplay=1"
So the iframe.setAttribute
function for the src URL is not working correctly. Any idea why, please?
Update 2:
I added this shortcut:
$ytubeURL = "https://www.youtube.com/embed/$ytubeId?rel=0&showinfo=0&autoplay=1";
and changed the JavaScript to:
( function() {
var youtube = document.querySelectorAll( ".ytcover" );
for (var i = 0; i < youtube.length; i++) {
youtube[i].addEventListener( "click", function() {
var iframe = document.createElement( "iframe" );
iframe.setAttribute( "frameborder", "0" );
iframe.setAttribute( "allowfullscreen", "" );
iframe.setAttribute( "src", "<?php echo $ytubeURL; ?>" );
this.innerHTML = "";
this.appendChild( iframe );
} );
};
} )();
When the html source code is viewed, the full URL is displayed in the script, but when clicking the link on the front end, and viewing via developer tools, the actual URL is still missing the YouTube ID: