Is it possible to create an iframe when a button is clicked using javascript

I have a user interface with a print button, I want to create an iframe when I click the print button, is it possible to use javascript?

+4
source share
3 answers
document.getElementById('print-button').onclick = function() { var iframe = document.createElement('iframe'); iframe.src = 'http://example.com'; document.body.appendChild(iframe); }; 

Of course, if you intend to attach more events of the same type, use addEventListener() .

If jQuery is at your disposal ...

 $("#print-button").click(function() { $("<iframe />", { src: "http://example.com" }).appendTo("body"); }); 
+4
source

Of course ... # printbutton is the identifier of your print button. A.

 <a href="#" id="printbutton">Print</a> <div id="iframe_parent"></div> <script src="PATH-to-JQUERY"></script> <script> $('#printbutton').click(function(){ var iframe = document.createElement('iframe'); iframe.setAttribute('src', 'path/to/iframe/page/'); document.getElementById('iframe_parent').appendChild(iframe); }); </script> 
+2
source
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $('#button').click(function(){ if(!$('#iframe').length) { $('#iframeHolder').html('<iframe id="iframe" src="http://w3schools.com" width="700" height="450"></iframe>'); } }); }); </script> <a id="button">Button</a> <div id="iframeHolder"></div> 
0
source

Source: https://habr.com/ru/post/1344040/


All Articles