Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an IFRAME using JavaScript

Tags:

javascript

I have a webpage hosted online and I would like it to be possible that I could insert an IFRAME onto another webpage using some JavaScript.

How would this be the best way possible, that I just add my webpage URL to the JavaScript and that it work on all browsers?

Thanks

like image 834
Tim Avatar asked Jan 04 '12 11:01

Tim


People also ask

Can you use javascript in an iframe?

As long as the protocol, domain and port of the parent page and iframe match, everything will work fine. I added an example on how to use the window.

How do I create an iframe?

To generate iframe, you need to set a URL to embed, width and height, scroll, disable or enable border, specify border type, size and border color, and add the iframe Name. Then, click the “Create iframe“ button to generate HTML code and push the “Copy To Clipboard“ button for copying the result.

What is iframe in Javascript?

An inline frame (iframe) is a HTML element that loads another HTML page within the document. It essentially puts another webpage within the parent page. They are commonly used for advertisements, embedded videos, web analytics and interactive content.

Are IFrames being phased out?

IFrames are not obsolete, but the reasons for using them are rare. Using IFrames to serve your own content creates a "wall" around accessing the content in that area. For crawlers like Google, It's not immediately clear that cotent in an iframe will be ranked as highly as if the content were simply part of the page.


2 Answers

You can use:

<script type="text/javascript">     function prepareFrame() {         var ifrm = document.createElement("iframe");         ifrm.setAttribute("src", "http://google.com/");         ifrm.style.width = "640px";         ifrm.style.height = "480px";         document.body.appendChild(ifrm);     } </script>  

also check basics of the iFrame element

like image 130
Hemant Metalia Avatar answered Sep 16 '22 12:09

Hemant Metalia


It is better to process HTML as a template than to build nodes via JavaScript (HTML is not XML after all.) You can keep your IFRAME's HTML syntax clean by using a template and then appending the template's contents into another DIV.

<div id="placeholder"></div>  <script id="iframeTemplate" type="text/html">     <iframe src="...">         <!-- replace this line with alternate content -->     </iframe> </script>  <script type="text/javascript"> var element,     html,     template;  element = document.getElementById("placeholder"); template = document.getElementById("iframeTemplate"); html = template.innerHTML;  element.innerHTML = html; </script> 
like image 37
rxgx Avatar answered Sep 18 '22 12:09

rxgx