How do I center or resize a Browser Window?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonieko@hotmail.com

    #1

    How do I center or resize a Browser Window?

    You can try this code:[color=blue]
    >
    >
    >
    >
    >[/color]
    <!--- filename: mypage.html ---->
    <html>
    ....blah blah blah
    <script language=JavaSc ript src=/Javascript/center.js></script>
    </html>



    /*************** *************** *
    filename: center.js
    *************** *************** **/


    var xMax = screen.width, yMax = self.screen.ava ilHeight;

    if (document.body. clientWidth)
    {
    lnBodyClientWid th=document.bod y.clientWidth
    lnBodyClientHei ght=document.bo dy.clientHeight
    }
    else
    {
    lnBodyClientWid th=window.inner Width
    lnBodyClientHei ght=window.inne rHeight
    }

    xOffset = (xMax - lnBodyClientWid th)/2, yOffset = (yMax -
    lnBodyClientHei ght)/2;
    self.moveTo(xOf fset,yOffset)







    <!-- javascript size resize browser client width window offset move -->

  • Richard Cornford

    #2
    Re: How do I center or resize a Browser Window?

    anonieko@hotmai l.com wrote:[color=blue]
    > You can try this code:[/color]
    <snip>[color=blue]
    > <script language=JavaSc ript src=/Javascript/center.js></script>[/color]
    <snip>

    In valid HTML 4 the TYPE attribute is required in an opening script tag.
    The LANGUAGE attribute is deprecated, and redundant when the required
    TYPE attribute is present.
    [color=blue]
    > var xMax = screen.width, yMax = self.screen.ava ilHeight;[/color]

    Using a mixture of - screen.width - and screen.availHei ght - seems to
    pre-suppose that the (a) taskbar is present on the system, that there
    are no other similar desktop features (office bar) and/or that it is
    (they are) located only at the top and/or bottom of the user's screen.

    That is a lot of assumptions, which will be wrong in some contexts (as
    desktop furniture can be user positioned).

    Not to mention the assumption that - availWidth/Height - are implemented
    in the environment in question and that their values are meaningful.
    There is also a disregard for the nature of these values on
    multi-monitor systems, where screen dimensions may be either one of the
    dimensions of the smallest rectangle that can encompass all of the
    monitors in use, or the dimensions of only the 'primary' monitor. in the
    first case positioning in a guessed 'centre' may result in the browser
    being placed over the boundary between two (or more) monitors, and/or in
    (or partly in) an area of the overall desktop that is not displayable.

    In the latter case the 'primary' monitor does not have to be the one on
    which the user is viewing the browser, leaving any 'centring' code
    possibly moving the entire browser out of immediate sight.
    [color=blue]
    > if (document.body. clientWidth)[/color]

    It would make sense to verify that - document.body - exists before
    accessing its properties, as it did not exist on older browsers so
    without the test the code will error out and fail uncontrollably.

    On IE, whether the - clientWidth - property reflects the width of the
    viewport depends on which mode the browser is in. In 'CSS1Compat' mode
    the viewport dimensions should be read from the -
    document.docume ntElement - property. For other DOM browsers that switch
    modes, the object from which the viewport dimensions should be read
    might be either one of - documentElement - or - body -, and the object
    in question may or may not switch when different modes are used.

    Also, type-converting test on DOM properties that are of primitive type
    (like numbers) result in false for certain values of those properties,
    even when the property is defined on the system. Testing with - typeof -
    is invariable more useful when properties may have primitive values when
    implemented.
    [color=blue]
    > {
    > lnBodyClientWid th=document.bod y.clientWidth
    > lnBodyClientHei ght=document.bo dy.clientHeight
    > }
    > else[/color]

    In circumstances where - document.body.c lientWidth - is undefined or
    zero there is no certainty that the system will support
    innerWidth/Height, so an additional test is indicated before this branch
    is taken.
    [color=blue]
    > {
    > lnBodyClientWid th=window.inner Width
    > lnBodyClientHei ght=window.inne rHeight
    > }
    >
    > xOffset = (xMax - lnBodyClientWid th)/2, yOffset = (yMax -
    > lnBodyClientHei ght)/2;[/color]

    The viewport dimensions are being used here without regard for the
    actual dimensions of the window. No account is being taken of the
    variable, and user configurable, window chrome. Having failed to take
    chrome into account, these positions will tend to overshoot the centre.
    There is also no account being taken for the possibility that the window
    dimensions already exceed the available screen dimensions, where the
    resulting negative offset may be very inconvenient to the user (if the
    browser acted upon them). And no account has been taken for taskbars and
    other desktop furniture being at the top or left of the screen(s), as
    that would suggest an offset for the positioning.

    However, as preceding code may have failed to assign a numeric value to
    either of - lnBodyClientWid th - or - lnBodyClientHei ght - verification
    of an acceptable range for those values should be extended to verify
    that they are meaningful numbers to start with.
    [color=blue]
    > self.moveTo(xOf fset,yOffset)[/color]

    The window's - moveTo - method has been subject to considerable abuse
    over the years so these days it is usually capable of being disabled by
    users, or disabled by default.
    [color=blue]
    > <!-- javascript size [ ... ] offset move -->[/color]

    In javascript these sequences are syntax errors, they should never
    appear in an javascript file.

    There a lots of things wrong with this code, and circumstances where it
    will fail (to some degree or another), but you have failed to state you
    question, or give any details of the problems you are actually
    experiencing with the code.

    Richard.


    Comment

    • anonieko@hotmail.com

      #3
      Re: How do I center or resize a Browser Window?

      Wow Thats a lot of pounding there! But where is your solution?

      Comment

      • Richard Cornford

        #4
        Re: How do I center or resize a Browser Window?

        anonieko@hotmai l.com wrote:[color=blue]
        > Wow Thats a lot of pounding there!
        > But where is your solution?[/color]

        Solution to what? You are still to state the problem and its context.
        You might try reading the group's FAQ, and related resources, on the
        subject of asking questions (among other things).

        Richard.


        Comment

        Working...