How does the DiggBar dynamically resize its iframe's height based on content not on their domain?
Asked Answered
L

5

6

Someone has already asked, How does the DiggBar work? in a previous question.

While someone provided a decent answer it didn't address one thing:

How does Digg dynamically resize their iframe's height, based on the content of a site across a different domain?

There are plenty of questions and answers here on SO for dynamically adjusting an iframes height based off content (using javascript) as long as the framed url is on your own domain. However, Digg seems to have solved this problem with websites of any domain.

Do any SO web programmers have any idea how they accomplished that?

Note: The iframe is NOT simply set to 100% height. The iframe tag simply does not work like that. Google "100% height iframe" and you'll see what I mean.

Lido answered 16/4, 2009 at 2:23 Comment(0)
L
18

If you look at their CSS, they use height: 100% for the iframe:

iframe#diggiFrame {
    color: #666;
    width: 100%;
    height: 100%;
    z-index: 10;
    -webkit-box-sizing: border-box;    
}

They position the DiggBar above that with a height of 46px, so the iframe takes 100% of the remaining space. They use overflow: hidden on the body element to keep the iframe entirely within the vertical height of the page, rather than allowing the page to scroll. This means that the scroll bar will then appear inside the iframe, instead of for the whole page. Note that the the way the DiggBar does it works only in quirks mode in Firefox; see below for how to do it in standards mode.

body {
    padding: 46px 0 0 0;
    margin: 0;
    background: #fff;
    overflow: hidden; 
    color: #333;
    text-align: left;
}

#t {
    width: 100%;
    min-width: 950px;
    height: 46px;
    z-index: 100;
    position: absolute;
    top: 0;
    left: 0;
    /* overflow: hidden; */
    border-bottom: 1px solid #666;
    background: #fff url(/App_PermaFrame/media/img/back.gif) repeat-x;
    line-height: 1;
}

edit: For those who don't believe me, here is a small example. To get it to fill the entire space, you need to set it to have no border, and you need <body> to have no margins.

edit 2: Ah, sorry, I see what you were talking about. You need the overflow: hidden on the body tag to get the scroll bar to work the way you want.

edit 3: It looks like you have to be in quirks mode for this to work in Firefox; if you include a <!DOCTYPE html> declaration, that puts you into standards mode, and your iframe comes out too small.

edit 4: Ah, you can do it in standards mode in Firefox as well. Got the answer here. You need to set the height on your <html> and <body> elements to 100% as well. (Note that the <!DOCTYPE html> is the doctype for HTML 5, which is a work in progress; however, it works on all modern browsers for turning on standards mode).

<!DOCTYPE html>
<html>
<head>
  <style type="text/css" media="all">
    html, body {
      height: 100%
    }
    body {
      margin: 0;
      overflow: hidden;
    }
    #topbar {
      height: 50px;
      width: 100%;
      border-bottom: 1px solid #666
    }
    #iframe {
      height: 100%;
      width: 100%;
      border-width: 0
    }
  </style>
</head>
<body>
  <div id="topbar">
    <h1>This is my fake DiggBar</h1>
  </div>
  <iframe id="iframe" src="http://www.google.com/"></iframe>
</body>
Lubricate answered 16/4, 2009 at 2:45 Comment(16)
iframes don't work liek that. Try it your self, set an iframe with height: 100% and set its src to gooogle.com. It won't work.March
Most people end up resorting to javascript to do this but it fails when trying to do it for a iframe with a src set to a site across a different domain. I'm curious how they do this too.March
height 100% will expand it to the height of the body tag, which isn't the height of the iframed content and in most cases will be too small.March
@brian campbell, look at this code: pastebin.com/m6f696be5 and this screenshot: i44.tinypic.com/257odja.pngLido
@Brian Campbell, do you see what I mean?Lido
Yep, I see what you mean. It turns out the reason for the difference is... the DOCTYPE! It looks like it renders the way you want it to in quirks mode (what you get if you don't include a DOCTYPE), but does not work if you are in standards mode (which you get from <!DOCTYPE html>).Lubricate
Also, you need to remove the position: fixed from your #toolbar. If you do that, then the iframe will wind up behind your bar, instead of flowing underneath it.Lubricate
Oh, and by the way: many people consider it very rude to frame their websites. There was a lot of protest against the DiggBar, to the point where Digg backed off and said they would stop using the DiggBar for anonymous users blog.digg.com/?p=664Lubricate
@Brian Campbell, this isn't going into production and I agree with the DiggBar protests. I just want to mimic it for a site a friend and I are making to learn MVC/jQuery from.Lido
@Lido Not a problem! I just wanted to warn you that some people find it rude; that doesn't mean you should never use it, just be judicious about when you do (and for testing & learning purposes, its fine)Lubricate
@Brian Campbell, so how taboo is it to run in quirks mode?Lido
@Lido It's generally a bad idea to run in quirks mode, since you never really know what you're going to get across different browsers. Luckily, I found a solution here (listed in my edit 4 above) that works in standards mode in Firefox.Lubricate
@Brian Campbell, have you tried this solution with a taller website? Try Yahoo for example. The scroll bar gets cut off at the bottom. I dig (heh) your solution so far, but have you run across this problem yet?Raddle
@Mike Robinson I have not tested it extensively. You can see through my history of edits the trial and error I went through. If the current solution does not work on some sites, did you try my previous solution that relies on quirks mode? This is what the actual DiggBar does. I don't have time to look into it now, but if that doesn't work, I'll take another stab at it and see if I can come up with something.Lubricate
It's a very nice solution. We are going to use it for embedding intranet applications in a portal.Froehlich
It has one issue, though: If the content is larger than the iframe size, a tiny portion of the content at the bottom is cut off. To demonstrate, replace google in the example link with e.g. cnn.com. A tiny portion of the page's footer gets cut off. pastebin.com/Txpzsi19 contains a tiny jQuery program that resizes the iframe relatively, while keeping the height of the top bar constant.Froehlich
W
2

Part of the problem with HTML you can't just set an element of any thing to 100% height and have it take up the whole window space. One way to do this is by making the body have a pixel height of the window and any thing inside the body set to 100% will be the size of the window.

Basically just make a JavaScript that ties onto the windows onresize event and have it resize the body to to the size of the window.

here is an example I made using jQuery

<script language="JavaScript" type="text/JavaScript">
    $(window).resize(function() {
        $('body').height(document.documentElement.clientHeight);
    });
</script>

With this you will be able to set a div or another elements and have it work at the full height of the window.

Wigeon answered 16/4, 2009 at 2:55 Comment(0)
G
1

An iframe can have height 100% in quirks mode. Digg achieves this by leaving out the doctype.

Grider answered 2/12, 2009 at 1:36 Comment(0)
S
0

The iFrame is on the Digg website with the target website inside, not the other way around. The iFrame is set to 100% width and height.

Scary answered 16/4, 2009 at 2:29 Comment(1)
Yes, I know this. But the src that iframe is pointing to is pointing to a URL outside their domain. Thus, because of security they shouldn't be able to dynamically resize it based of that content height.Lido
M
0

100% in an iframe is a 100% percent of the declared parent space. An example is:

/* parent element */

html, body {
   width: 100%;
   height: 100%;
}

/* child element */
iframe {
  width: 100%; /* this is truly 100%, try it out */
  height: 100%; /* try it out */
Marandamarasca answered 14/3, 2014 at 23:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.