On my site, when I click on the links, I need to display a modal pop-up window (see also this solution ), which:
- sometimes showing an external website (link # 1 below)
- sometimes only shows jpg image (link # 2 below)
Problem: when displaying:
external site in a modal pop-up window, size is fine (see link # 1)
jpeg image in a modal pop-up window, the size does not fit , in the sense that it does not adapt to the screen (see link # 2):

- jpeg image in a new window, the size is in order, automatically adapted to the screen (see link No. 3).

Question: How to make a JPG image displayed in the iframe modal pop-up menu automatically adapts its size , i.e. if the jpg height is high, does it automatically change? that is, exactly the same as this happens when the image is displayed in a new window (see link # 3)
PS: do not forget to enable the loading of unsafe scripts in the browser when trying this piece of code. If not, iframes will not be displayed.
Or use this live jsfiddle demo.
document.getElementById("link1").onclick = function(e) { e.preventDefault(); document.getElementById("popupdarkbg").style.display = "block"; document.getElementById("popup").style.display = "block"; document.getElementById('popupiframe').src = "http://example.com"; document.getElementById('popupdarkbg').onclick = function() { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; }; return false; } document.getElementById("link2").onclick = function(e) { e.preventDefault(); document.getElementById("popupdarkbg").style.display = "block"; document.getElementById("popup").style.display = "block"; document.getElementById('popupiframe').src = "https://i.imgur.com/pz2iPBW.jpg"; document.getElementById('popupdarkbg').onclick = function() { document.getElementById("popup").style.display = "none"; document.getElementById("popupdarkbg").style.display = "none"; }; return false; } document.getElementById("link3").onclick = function(e) { window.open('https://i.imgur.com/pz2iPBW.jpg', 'newwindow', 'width=700,height=500'); e.preventDefault(); return false; }
#popup { display: none; position: fixed; top: 12%; left: 15%; width: 70%; height: 70%; background-color: white; z-index: 10; } #popup iframe { width: 100%; height: 100%; border: 0; } #popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: rgba(0,0,0,.75); display: none; }
<div id="main"> <a href="" id="link1">Click me (website, modal popup)</a><br> <a href="" id="link2">Click me (jpg image, modal popup)</a><br> <a href="" id="link3">Click me (jpg image, new window)</a><br> </div> <div id="popup"><iframe id="popupiframe"></iframe></div> <div id="popupdarkbg"></div>
source share