Javascript Element How to - Create iframe and load its content








Question

We would like to know how to create iframe and load its content.

Answer


<!DOCTYPE html>
<html>
<body>
  <div id="wrapper"></div>
<script type='text/javascript'>
<!--from   w  ww  .  j a v a  2s.c  o  m-->
    var frame = document.createElement('iframe');
    frame.src="http://www.cnn.com";
    document.getElementById('wrapper').appendChild(frame);

</script>
</body>
</html>

The code above is rendered as follows: