Placing a dynamic Youtube video in an <iframe> (Asp.net)

Asked

Viewed 567 times

1

Good night! I am trying to put a youtube video based on a url saved in an sql database, the problem is that the way I thought of putting, works only with some static url, in the following way:

<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, I thought of a label and then assign it to . Text, but it doesn’t work because Iframe stops when I insert the label in the middle of it ... does anyone have any idea what I can do?

Thank you :D

1 answer

1

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

Example 1: use the src attribute of the iframe.

// 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 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.