Putting a dynamic YouTube video into one (asp.net)

Good night! I'm trying to put a youtube video based on a url saved in a sql database, the problem is that the way I found to put it, it only works with some static url, in the following Mode:

<iframe width="560" height="315" src="https://www.youtube.com/embed/o_l4Ab5FRwM" frameborder="0" allowfullscreen></iframe>

I'm trying to put a variable value for src, thought of a label and then assign no .Text, but it does not work because the iframe stops when I insert the label in the middle of it ... does anyone have any idea what I can do?

Thank you : D

Author: Gabriel Gonçalves, 2016-05-18

1 answers

Is this variable server-side or client-side? In these cases you have two Solutions:

Example 1: use the iframe src attribute.

// Captura elementos iframe, pode utilizar o método getElementById
var iframe = document.getElementsByTagName("iframe");
iframe[0].src = "https://www.youtube.com/embed/o_l4Ab5FRwM";
<iframe width="560" height="315" src="" frameborder="0" allowfullscreen></iframe>

Example 2 : build the iframe dynamically via javascript.

var iframe2 = document.createElement("iframe");
iframe2.src = "https://www.youtube.com/embed/o_l4Ab5FRwM";
iframe2.width = 560;
iframe2.height = 315;
iframe2.frameborder = 0;
iframe2.allowfullscreen = true;
document.body.appendChild(iframe2);
 1
Author: Gabriel Gonçalves, 2016-05-18 02:55:09