0

I'm using the following code to monitor screen size change and run alertSize() but it only works when loading the page and then it doesn't work

   window.onresize=alertSize();
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66

1 Answers1

1

You want

window.onresize = alertSize;

You need to set the "onresize" property to refer to your function, not to the result of calling your function.

Actually you probably shouldn't try this, as it will be very painful to test if your function really is using alert(). The browser fires many "resize" events while a window is being resized on a desktop computer.

You might also want to look into CSS media queries.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • thank you so much for the great answer. it works. I will search for that "CSS media queries". all I want is changing some CSS depending on screen size. – Sami Al-Subhi Apr 29 '12 at 14:00
  • [Here](https://developer.mozilla.org/en/CSS/Media_queries) is the Mozilla documentation page about media queries. They're supported on most modern mobile devices I think, but I'm not an expert. – Pointy Apr 29 '12 at 14:02