0

I have a JS variable that holds html code for embedding a YouTube video, which looks like:

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

Is there any way to simply read the html from the variable? Or must I extract the URL from the variable and surround it with the iframe tag framework?

3 Answers3

0

If you're talking about reading the HTML code inside the iframe, then you can't do that with JavaScript: take a look here for more information.

If you want to add the frame on your document you can do it using the innerHTML property of an element, for example:

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

document.getElementById("container-id").innerHTML += myFrame;
Community
  • 1
  • 1
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
0

If your HTML have following tag you need to add "id" as a attribute.

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

You can read properties in this way:

document.getElementById('iframe1').getAttribute('frameborder')
Raptor
  • 53,206
  • 45
  • 230
  • 366
Bhuneshwer
  • 577
  • 1
  • 9
  • 26
0

Here's a working code

<body>
<div id="DIV">
</div>
<script type="text/javascript">    
   var content = document.getElementById('DIV');
   var frame = "<iframe width='560' height='315' src='https://www.youtube.com/embed/.....'
frameborder='0' allowfullscreen></iframe>"
   content.innerHTML = frame ;   
</script>
</body>
Neji Soltani
  • 1,522
  • 4
  • 22
  • 41