Browser compatibility for centering function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Wigzell

    #1

    Browser compatibility for centering function

    I have the following funciton that centers my website content for any size window and will center it in real time as the window is expanded or shrunk. It is activated by a

    onresize="Cente rIt();"

    in the body tag.

    Works fine for IE. How do I make it compatible with netscape and most browsers? Better yet, is there a good single source that explains how to write javascript to be compatible with all browsers? 766 and 435 are the width and height of my table that surrounds the website data defined so:

    <TABLE id="Main" style="position :absolute; z-index:0; top:0; left:0;">

    function CenterIt()
    {
    newOffsetWidth = 0;
    newOffsetHeight = 0;
    if (document.getEl ementById)
    {
    winW = document.body.o ffsetWidth;
    winH = document.body.o ffsetHeight;
    }
    else if (document.all)
    {
    /*
    What goes here????
    */
    }
    else if (document.layer s)
    {
    /*
    What goes here????
    */
    }

    if (winW > 766)
    newOffsetWidth = ((winW - 766) / 2) - 10;
    if (winH > 435)
    newOffsetHeight = ((winH - 435) / 2);

    if (document.getEl ementById)
    {
    document.getEle mentById('Main' ).style.left = newOffsetWidth;
    document.getEle mentById('Main' ).style.top = newOffsetHeight ;
    }
    else if (document.all)
    {
    /*
    What goes here????
    */
    }
    else if (document.layer s)
    {
    /*
    What goes here????
    */
    }

    }
    CenterIt();

    Thanks!
  • kaeli

    #2
    Re: Browser compatibility for centering function

    In article <OfUIc.6646$Mr4 .1524@pd7tw1no> , simonwigzell@sh aw.ca
    enlightened us with...[color=blue]
    > I have the following funciton that centers my website content for any size
    > window and will center it in real time as the window is expanded or shrunk.[/color]

    Why? All you need is a good stylesheet and decent design to accomplish
    this in all browsers, and it won't break when a user disables script
    [1].


    [1] NN4 has a problem with disabling CSS/script. Too tightly integrated,
    disabling one tends to break both.

    --
    --
    ~kaeli~
    A hangover is the wrath of grapes.



    Comment

    • Andrew Thompson

      #3
      Re: Browser compatibility for centering function

      On Tue, 13 Jul 2004 12:52:33 -0500, kaeli wrote:
      [color=blue]
      > [1] NN4 has a problem with disabling CSS/script.[/color]

      ....I had a page with a background color and font
      defined in a stylesheet. No script, just HTML
      elements (an <H1> and <P> elements AFAIR).

      NN4 did not retain the font all the way to
      the bottom of the page, instead it reverted
      from the sans serif font I'd specified, to
      it's own default serif. [ Though it did
      render the BG color reliably.. ;-) ]

      I validated the very simple HTML and CSS..
      both fine. Never figured it out, could no
      longer be bothered.

      --
      Andrew Thompson
      http://www.PhySci.org/ Open-source software suite
      http://www.PhySci.org/codes/ Web & IT Help
      http://www.1point1C.org/ Science & Technology

      Comment

      • kaeli

        #4
        Re: Browser compatibility for centering function

        In article <ho1xcbp0nx1v.o 0we26a88pdf.dlg @40tude.net>,
        SeeMySites@www. invalid enlightened us with...[color=blue]
        > On Tue, 13 Jul 2004 12:52:33 -0500, kaeli wrote:
        >[color=green]
        > > [1] NN4 has a problem with disabling CSS/script.[/color]
        >
        > ...I had a page with a background color and font
        > defined in a stylesheet. No script, just HTML
        > elements (an <H1> and <P> elements AFAIR).
        >
        > NN4 did not retain the font all the way to
        > the bottom of the page, instead it reverted
        > from the sans serif font I'd specified, to
        > it's own default serif. [ Though it did
        > render the BG color reliably.. ;-) ]
        >
        > I validated the very simple HTML and CSS..
        > both fine. Never figured it out, could no
        > longer be bothered.
        >
        >[/color]

        If you tried to style the font in the body instead of each element,
        IIRC, that was a known NN4 bug with CSS inheritance.

        NN4 really has pretty terrible CSS support, but it can be coded around
        for some things. Most people who still have to support it import
        different stylesheets for NN4 and everything else, it's so bad. *g*
        I used tables for layout when I had to support it. I still use tables
        for layout sometimes, as CSS support is still not 100% cross-browser.

        But as far as just centering content (OP), it really doesn't get much
        easier than
        <div align="center">
        content
        </div>
        (the center tag is deprecated)

        If you've got really complicated stuff in there, sometimes the best is
        still
        <table width="90%" align="center" border="0" cellspacing="0"
        cellpadding="0" >
        <tr>
        <td valign="top" align="left">
        content
        </td>
        </tr>
        </table>
        (width is any percent you like and I think this does not work with a
        strict doctype, but IME few people use a strict doctype comparitively
        speaking.)

        If you had complicated enough CSS where you were playing with absolute
        positioning that would place the content outside the table, this
        question (OP) would be a moot point anyway.

        --
        --
        ~kaeli~
        A bicycle can't stand on its own because it is two tired.



        Comment

        • Simon Wigzell

          #5
          Re: Browser compatibility for centering function

          My function also centers vertically, in fact that was the main point. I guess I could forget the horizontal centering and just use a table align=center but that doesn't change my objective here or my question - how do I make this piece of code work for other browsers?

          Comment

          • Mike Foster

            #6
            Re: Browser compatibility for centering function

            Simon Wigzell wrote:[color=blue]
            > ...
            > Works fine for IE. How do I make it compatible with netscape and most browsers?
            > ...[/color]

            Cross-Browser DHTML Resource: http://cross-browser.com/

            :-)

            Comment

            • Dan Webb

              #7
              Re: Browser compatibility for centering function

              Hi,

              I've just written this script and it seems to work pretty well on the
              browsers Ive got installed (IE5+, Mozilla, Firefox, Opera on PC) so it
              might help you out. It does vertical centering only leaving the horiz
              centering to CSS (you can see an example of that in this page too).

              Theres no documentation Im afraid as its just hot of the keyboard but
              its fairly self explanatory (I hope)...



              Hope it helps,

              Dan

              Comment

              Working...