Skip navigation

well, i kind of stumbled with this for a few minutes, but if you’re trying to capture the resize event, you have to attach the listener to the WINDOW object and NOT the body.

This is WEIRD because the way w3schools has it, they attach it seemingly to the body.

<body onresize=" alert('resized'); ">

</body>

That method actually works. But if you need to dynamically attach/detach the resize event handler, then you’ll need to use javascript to do it.

So, here’s how you do it:

    window.onresize = function() {
       alert('resized');   
    };

And here’s how you DON’T do it:

    document.body.onresize/*WRONG!!*/ = function() {
       alert('resized');   
    };

There’s a nice forum post @ bytes.com with some weird code.

Advertisements

5 Comments

  1. You are right…but what does this have to do with Prototype?

  2. :) yeah. well, i originally had posted this using prototype js code.

    then i edited the post using normal js (looks simpler in this instance!) and forgot to change the title.

  3. You can find a real example of how to se the onresize even with prototype on my blog at http://robcos.com/onresize-event-and-prototype/

    • Filippo D.
    • Posted January 30, 2009 at 9:33 am
    • Permalink

    Thank you very much! I had the same problem, in few seconds I straightened it out!

    • Chohr
    • Posted July 12, 2011 at 2:44 pm
    • Permalink

    I’m guessing this overwrites any code that was previously assigned to the onresize event. Is there any way to add further code to this event without overwriting what was there before?


2 Trackbacks/Pingbacks

  1. […] since you will overwrite any other event listeners that can have been add by other libraries (Bobobobo has a bad example) Tags: Javascript, listener, onresize, prototype, resize event, window| Posted […]

  2. By ideacode Software on 06 Sep 2011 at 12:52 am

    window.resize + OpenLayers = full-screen maps…

    A persistent feature request for our GIS facilities management map has been “full screen mode.”  So, today, I sat down to tie together browser resize and OpenLayers map sizing.  I wasn’t sure off-hand if resize was a window, document, or b…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: