Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create an iframe on click of a button using javascript

I have a UI with a print button, i want to create an iframe on click of the print button, is it possible using javascript?

like image 983
user475685 Avatar asked Mar 17 '11 05:03

user475685


People also ask

Can JavaScript access an iframe?

In this article. Apart from accessing an element by adding its control to the Controls Repository, there is also the possibility to access an element within a web page's iframe via Javascript.

Can we add button in iframe?

The Button is a widget for iFrame granting you an opportunity to build various buttons for different purposes on your website. There are tons of actions that can be taken after a user clicks the button.


2 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're intending 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");
});
like image 71
alex Avatar answered Oct 02 '22 06:10

alex


Sure...#printbutton is the ID of your print button.

<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>
like image 44
Jeremy Conley Avatar answered Oct 02 '22 04:10

Jeremy Conley