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
  • 55,971
  • 20
  • 106
  • 115
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
  • 51,208
  • 43
  • 217
  • 353
Bhuneshwer
  • 547
  • 1
  • 9
  • 25
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,452
  • 3
  • 21
  • 39