2

I have an iframe in the same domain as the web page.

I have a header that I would like to keep the same height (I don't know what it is, let's say 80px), while the body contains the iframe, which is resized when the browser is resized:

---------- --------------
| header | | header |
---------- --------------
| iframe | -> | |
| | | |
| | | .... |
---------- | |
 | |
 --------------

I am using jQuery and have tried height() and css() on the iframe div, but those do not change the iframe's dimensions. The iframe remains at the same, small, default dimensions.

If I manually specify percentages in the iframe CSS, then resizing works, but because these are percentages, the iframe goes beyond the immediate page's borders and gets cut off after a high enough percent value pushes its borders off the screen.

I need some way to help the iframe compute its own borders by pixels, so that it stays within the borders of the page. I can grab the window width and height, but as mentioned, applying css() and height() from those values did not update the iframe's size.

Again: I am working with the iframe in the same domain as the parent page.

How would I properly code the resizing of both the header and iframe, so that a resize() operation (or resizing the browser window) actually works?

The code I have tried is something like the following:

<html>
<head>
... // load jquery, etc.
<script>
function resize() { 
 var w = $('#body').width(); 
 var h = $('#body').height();
 $('#header').width(w + "px"); // works
 $('#header').height("80px"); // works
 $('#my_iframe').css("width", w + "px"); // doesn't work
 $('#my_iframe').width(w + "px"); // doesn't work, either
 $('#my_iframe').css("height", (h-80) + "px"); // doesn't work
 $('#my_iframe').height((h-80) + "px"); // doesn't work, either
}
</script>
</head>
<body onresize="resize()" id="body">
<div id="header"><div>
<iframe id="my_iframe" .../>
</body>
</html>
asked Apr 5, 2012 at 10:12
2
  • So to confirm, are you setting dimensions on the iframe tag itself? I was confused when you said "on the iframe div". My approach would be to listen for window resize and then, in the event callback, set dimensions on the iframe tag equal to the body width and the body height minus the height of the header. Commented Apr 5, 2012 at 10:17
  • Are you trying to keep the page filled out with an iframe less an 80px section at the top? Or should the iframe be able to resize it's parent window? Commented Apr 5, 2012 at 10:19

1 Answer 1

2

Bind the resize event on the window, and set the height of the iframe:

$('#content').css('height', ($(window).height() - 80) + 'px');

Demo: http://jsfiddle.net/Guffa/t4b4j/

answered Apr 5, 2012 at 10:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.