[JavaScript]Displaying division at the bottom of browser window. Paragraph in footer shows after scrolling down

Rate It (1)

Last post 06-22-2007 1:16 PM by szmitek. 2 replies.

Sort Posts:

  • [JavaScript]Displaying division at the bottom of browser window. Paragraph in footer shows after scrolling down

    06-17-2007, 4:12 PM
    • Loading...
    • szmitek
    • Joined on 03-03-2007, 7:22 PM
    • Chotomów, Poland
    • Posts 59

    I would like to create a script in JavaScript language so that:

    • if page fills whole window of the browser, the division with footer is behind the rest of page normally;
    • if page does not fill whole window of the browser, the division is at the bottom of the window.

    I have made this script (http://szmitek.winweb.pl/footer.js). This is whole code of this script:

    function getWindowHeight() {
       var windowHeight = 0;
       if (typeof(window.innerHeight) == 'number') {
          windowHeight = window.innerHeight;
       }
       else {
          if (document.documentElement && document.documentElement.clientHeight) {
             windowHeight = document.documentElement.clientHeight;
          }
          else {
             if (document.body && document.body.clientHeight) {
                windowHeight = document.body.clientHeight;
             }
          }
       }
       return windowHeight;
    }
    function setFooter() {
       if (document.getElementById) {
          var windowHeight = getWindowHeight();
          if (windowHeight > 0) {

    // 'main' - id of division with everything beside footer (above footer)
             var contentHeight = document.getElementById('main').offsetHeight;

    //'footer' - id of division with footer
             var footerElement = document.getElementById('footer');
             var footerHeight  = footerElement.offsetHeight;
             if (windowHeight - (contentHeight + footerHeight) >= 0) {
                footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
             }
             else {
                footerElement.style.top = '0px';
             }
          }
       }
    }
    window.onload = function() {
       setFooter();
    }
    window.onresize = function() {
       setFooter();
    }

    At pages of my Web site, I have used it in this way:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">

    ...

    <link href="../format.css" rel="stylesheet" type="text/css" />

    ...

    <script type="text/javascript" language="javascript" src="../footer.js" mce_src="../footer.js"></script>

    ...

    <div id="main">
    ...
    </div>
     // '<div id="footer">' - the footer
    <div id="footer"><table cellpadding="0px" cellspacing="0px" class="e-mail"><tr><td><div class="e-mail">
    <p><span>E-mail:</span> <a href="mailto:szmitek@hotmail.com">szmitek@hotmail.com</a></p></div></td>
    </tr></table><p>
     ...
    </p></div>
    ...
    The problem is if page does not fill whole window of the browser (like my home page http://szmitek.winweb.pl normally):
    • the table in the footer (marked with green above) is corectly displayed at the bottom of the browser window
    • but a paragraph under this table (marked with red above) is visible (shows) after scrolling down.
    I want the page to be displayed with the paragraph under the table in footer without scrolling down
     if if page does not fill whole window of the browser. How to do that?
    This is extact of  stylesheet used on these pages (http://szmitek.winweb.pl/format.css):
    body {
    	margin: 0;
    	margin-top: 0;
    	background-color: #FFFFFF;
    }
    ...
    p {
    	font-family: Arial;
    	font-size: medium;
    	margin-left: 10px;
    	margin-right: 10px;
    }
    ...
    div#footer {
    	position: relative;
    	background-color: black;
    	margin: 0px;
    }
    div#footer p {
    	color: white;
    	margin: 0;
    	margin-top: 0px;
    	margin-left: 10px;
    	margin-right: 10px;
    }
    table.e-mail {
    	width: 100%;
    	margin-bottom: 0px;
    	height: 31px;
    	background-image: url('e-mail.png');
    	border: 0px;
    }
    div.e-mail p {
    	margin: 0;
    	margin-left: 10px;
    	margin-right: 10px;
    	margin-bottom: 0px;
    }
    ...
    I am asking for help politely.
  • Re: [JavaScript]Displaying division at the bottom of browser window. Paragraph in footer shows after scrolling down

    06-21-2007, 2:07 PM
    Answer
    • Loading...
    • mbischoff
    • Joined on 09-06-2005, 5:14 AM
    • Posts 113

    I'm not sure if this is the exact solution you need, but maybe the code below can serve as a start for you:

     

     

    <html><head><title>Test Page</title>
    <style>
    html, body
    {
    	height: 100%;
    	padding: 0;
    	margin: 0;
    	overflow: hidden;
    }
    #main
    {
    	background-color: grey;
    	overflow: auto;
    }
    #footer
    {
    	background-color: yellow;
    	text-align: center;
    }
    </style>
    <script type="text/javascript">
    window.onresize = resizeMainDiv;
    function resizeMainDiv()
    {
    	var main = document.getElementById("main");
    	var footer = document.getElementById("footer");
    	var winHeight = document.documentElement.offsetHeight;	
    	main.style.height = winHeight - footer.offsetHeight;
    }
    </script>
    </head>
    <body onload="resizeMainDiv();">
    <div id="main">
    content<br />goes<br />here<br />
    ...<br />...<br />...<br />...<br />...<br />...<br />...<br />
    more content<br />more content<br />more content<br />more content<br />more content<br />
    more content<br />more content<br />more content<br />more content<br />more content<br />
    </div>
    <div id="footer">
    this is the footer
    </div>
    </body>
    </html>
     
    ---
    Martin
    Please mark this post as Answer if it was helpful.

    -- Martin
  • This was error with CSS

    06-22-2007, 1:16 PM
    • Loading...
    • szmitek
    • Joined on 03-03-2007, 7:22 PM
    • Chotomów, Poland
    • Posts 59

    Thank you. This was error with CSS (new in red):

    body  {
     margin: 0;
     margin-top: 0;
     background-color: #FFFFFF;
     height:100%;
    }
    div#main {
     overflow: auto;
    }

    Now everything shows correctly without cutting off last line. See http://szmitek.winweb.pl

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter