1

What is the best practice creating an iframe HTML element using JavaScript?

 var iframe = document.createElement("iframe");
 iframe.setAttribute("sandbox", "")
 iframe.setAttribute("src", url);

I faced the question while developing Firefox plugin, but usage is quite the same as developing a web page. I do not use jQuery.

Dmitry Kaigorodov
  • 1,512
  • 18
  • 27

1 Answers1

2

You can try like this:-

<script type="text/javascript">
function abc() {
  iframe = document.createElement("IFRAME");
  iframe.setAttribute("src", "http://example.com/");
  iframe.style.width = "640px";
  iframe.style.height = "480px";
  document.body.appendChild(iframe);
}
</script> 
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • No problem ;) One question though, why do you write `640+"px"` instead of `"640px"` directly? – ComFreek Sep 02 '13 at 21:35
  • should I use `sandbox` attribute? Other HTML5 features? – Dmitry Kaigorodov Sep 02 '13 at 21:58
  • Yes you can use. The sandbox attribute enables a set of extra restrictions for the content in the inline frame. NOTE:- The sandbox attribute is not supported in Internet Explorer 9 and earlier versions, or in Opera. – Rahul Tripathi Sep 02 '13 at 22:01