javascript: detect window resize

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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/answers/430012-body-onresize-vs-window-onresize


Is there an sane way to detect an window resize using javascript ? :confused:
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
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.
 

slacker3

New Member
Messages
146
Reaction score
6
Points
0
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.
 

fomalhaut

Member
Messages
107
Reaction score
0
Points
16
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
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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);
 
Top