'Open Iframe in bootstrap modal on click
I am attempting to create a button that opens a modal containing an iframe. The iframe should only be loading once the modal opens. Right now when I click on my button to open the modal, nothing is loaded and no errors are given in console.
I am following an old snippet I found here but it seems with a newer version of bootstrap, it does not seem to work.
Jquery
var frameSrc = "http://test.com";
$('#ticketViewBtn').click(function(){
$('#ticketView').on('show', function () {
$('iframe').attr("src",frameSrc);
});
$('#ticketView').modal({show:true})
});
Bootstrap
<!-- Button trigger modal -->
<button class="btn btn-primary btn-sm" id="#ticketViewBtn" data-toggle="modal" data-target="#ticketView">
TICKET
</button>
<!-- Modal -->
<div class="modal fade" id="ticketView" tabindex="-1" role="dialog" aria-labelledby="ticketViewLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="ticketViewLabel">TICKET</h4>
</div>
<div class="modal-body">
<iframe src="" height="600px" width="800px" frameborder="0"></iframe>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
Solution 1:[1]
Use this code for load the iframe .
$('#ticketViewBtn').click(function(){
var iframe_id = $("#myiframe");
iframe.attr("src", iframe.data("src"));
$('#ticketView').modal({show:true})
});
<iframe id="myiframe" src="" height="600px" data-src="http://test.com" src="about:blank" width="800px" frameborder="0" onload="resizeIframe(this)" ></iframe>
function resizeIframe(iframe) {
iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
}
Solution 2:[2]
ok.. this is simple modal ..
no jquery and no bootstrap
just skeleton ..
Every link will 'target' into iframe_modal in modal div
.modal {
z-index:3;
display:none;
padding-top:50px;
position:fixed;
left:0;
top:0;
width:100%;
height:100%;
overflow:auto;
background-color:rgb(0,0,0);
background-color:rgba(0,0,0,0.4)
}
<a href="https://www.example.com" onclick="document.getElementById('modal1').style.display='block'" target="iframe_modal" >example</a>
<a href="https://www.bing.com" onclick="document.getElementById('modal1').style.display='block'" target="iframe_modal">bing</a>
<div id="modal1" class="modal">
<span onclick="document.getElementById('modal1').style.display='none'; document.getElementById('iframe1').src =''" class="">×</span>
<iframe id="iframe1" height="300px" width="100%" src="" name="iframe_modal"></iframe>
</div>
Using jquery:
<a href="https://www.example.com">example</a>
<a href="https://bing.com/" >bing</a>
<div id="modal1" class="modal">
<span>×</span>
<iframe id="iframe1" height="300px" width="100%" src="" ></iframe>
</div>
$(document).ready(function(){
$("a").click(function(){
$("#modal1").show();
when click an 'a', current (this) atribute href 'a' will be get and set into src '#iframe1'
$("#iframe1").attr("src", $(this).attr("href"));
return false;
});
$("span").click(function(){
$("#modal1").hide();
$("#iframe1").attr("src", "");
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
