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.

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

3 Comments

No problem ;) One question though, why do you write 640+"px" instead of "640px" directly?
should I use sandbox attribute? Other HTML5 features?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.