+ Reply to Thread
Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By slacker3
  • 1 Post By fomalhaut

Thread: javascript: detect window resize

  1. #1
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    javascript: detect window resize

    Tony wrote:

    > If I want to call a function (call it doResize) when the browser
    > window is resized, is it better to put that in the body tag:
    >
    > <body onresize="doResize()">
    >
    > Or is it better to put the call in a script tag in the <head>:
    >
    > <script type="text/javascript">
    > window.onresize = doResize;
    > </script>
    >
    > Or, is there no practical difference between the two?


    There is no `onresize' attribute for the `body' element in Valid HTML:
    <URL:http://validator.w3.org/>

    `onresize' is a proprietary event handler of the proprietary host object
    `window' refers to.

    The latter will either work, break on runtime or have no effect at all,
    while the former will break in an SGML parser, and may work or have no
    effect at all.

    <URL:http://www.pointedears.de/scripts/test/whatami>


    PointedEars
    http://bytes.com/topic/javascript/an...indow-onresize


    Is there an sane way to detect an window resize using javascript ?
    dinomirt96 likes this.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: javascript: detect window resize

    Quote Originally Posted by slacker3 View Post

    There is no `onresize' attribute for the `body' element in Valid HTML:
    <URL:http://validator.w3.org/>

    `onresize' is a proprietary event handler of the proprietary host object
    `window' refers to
    .
    What he is saying is that you cannot assign an onsize event handler to the body element, but instead you have to add it to the window object, ie

    window.onresize = doSomething

    like you assign it to to window.onload or window.onunload handlers.
    Nothing is always absolutely so.

  3. #3
    slacker3 is offline x10 Sophmore slacker3 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    146

    Re: javascript: detect window resize

    Strangely enough, <body onresize="fun()"> was the first google came up with and it actually worked when i tried it. But the validator said no.

    Well..
    The latter will either work, break on runtime or have no effect at all,
    while the former will break in an SGML parser, and may work or have no
    effect at all.
    This sentence confused me a little bit.


    Whenever i search the web for simple javascript questions i get outdated
    or even contradicting results. I guess i will stick with mozilla's reference now:
    https://developer.mozilla.org/En/DOM/Window.onresize
    (just found)


    Thanks for your reply.

  4. #4
    fomalhaut is offline x10Hosting Member fomalhaut is an unknown quantity at this point
    Join Date
    Aug 2009
    Location
    South of France near Arles
    Posts
    91

    Re: javascript: detect window resize

    Hello

    I think the onresize is supported by the "<body>" HTML tag - ans other (e.g. <div>, <fieldset>, <frame>, ecc...) but ONLY by the JAVASCRIPT object "window" - i.e. window.onresize, as explained at http://www.w3schools.com/jsref/event_onresize.asp

    I've often use it in the two ways without any problem. This is the hierarchy : Window - Document - Body. So, I think that body is a short cut for window.document.body
    karimirt47 likes this.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: javascript: detect window resize

    Quote Originally Posted by fomalhaut View Post
    So, I think that body is a short cut for window.document.body
    In JS hosted by a browser, global variables (other than window) are resolved as properties of window (as if all code blocks were surrounded by an implicit "with (window)"). document is thus window.document. A reference to body, by itself, is undefined. In a global context, try:
    Code:
    var foo='bar';
    alert(window.foo);
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Javascript New Window Settings
    By secretply in forum Programming Help
    Replies: 1
    Last Post: 05-17-2009, 11:09 AM
  2. Javascript - Targeting a window by name from a function?
    By onyxmoron in forum Programming Help
    Replies: 5
    Last Post: 01-12-2009, 05:56 AM
  3. javascript help - window width
    By kbjradmin in forum Programming Help
    Replies: 7
    Last Post: 10-30-2008, 06:39 PM
  4. JavaScript - Resize iFrame
    By nunoabc in forum Programming Help
    Replies: 1
    Last Post: 06-23-2008, 12:54 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers