Ask a Jedi: Sizing a window with ColdFusion

Jay asks:

OK, I should know how to do this and I feel stupid for asking but I am going to anyway.

Is there a simple way in Coldfusion to grab the current size of the window you have opened?

This isn't stupid - but is one of the many questions that reveal that you may have forgotten that ColdFusion is completely server side. ColdFusion's only interaction with the browser is with the HTML returned via the web server.

So with that being said, you can use JavaScript to check the size of the window. I found a few methods, but these properties seem to work fine in Firefox. (And I'm too lazy to start Parallels just for IE, so I'm fine with people correcting me.)

window.outerWidth
window.outerHeight

You can check these values and if they are too small, resize the window. Consider this complete example:

<html>

<head>
<title>Min Size Test</title>
<script>
function checkMinSize() {
   if(window.outerWidth < 500) window.resizeTo(500, window.outerHeight);
   if(window.outerHeight < 500) window.resizeTo(window.outerWidth,500);
}
</script>
</head>

<body onLoad="checkMinSize()">

</body>
</html>

All this does is check the width and height. If either are less then 500 pixels, the window is resized to the correct size. I do this in two steps because it is possible only one dimension is too small.

Comments

Just to clarify, there's no reason that you couldn't use AJAX to pass in the window dimensions to Coldfusion, or a self-posting form for that matter.
# Posted By Andy Matthews | 5/7/07 10:52 PM
Correct, but you would need that initial request to load first.
# Posted By Raymond Camden | 5/7/07 10:57 PM
There's also the technique of creating a web bug: passing url parameters to an image tag/ColdFusion template via document.write that returns a 1x1.gif to the browser. (What we all used to do before the XmlHttpRequest object.) From there you can store the window dimension values as cookie, client, or session vars to pick up later. Granted, once again, you're still limited to some sort of event (like body onLoad or user action) to trigger the window change.
# Posted By Erik Yowell | 5/8/07 6:53 AM
It should also be pointed out that with so many people using firefox and opening windows in tabs, when you resize a window, it will just resize the whole browser. Window resizing is so 2001 :)
# Posted By David Herman | 5/8/07 8:14 AM
It's also a bit of an useability no-no (and accessibility etc.)
# Posted By Tom K | 5/8/07 8:18 AM
I agree with you guys - but I believe the original poster was talking about a popup, not the main window.
# Posted By Raymond Camden | 5/8/07 8:24 AM
He probably was, but I don't do popups, I have firefox open them all in new tabs, which then resized the whole browser...
# Posted By David Herman | 5/8/07 9:08 AM
This information could also be useful in some AJAX or plain old CSS "popup" situations (where a behavior turns a loaded but invisible div visible), by letting you know how much space to give the popup or where / how to place it.

For instance, if I had an invisible div with Ray's picture and bio that showed visible when the mouse hovered over his name, I could put the photo side by side with the bio on a wider browser window, and skip the photo and just have the bio on a smaller window.
# Posted By James Edmunds | 5/8/07 9:09 AM
I appreciate all your thoughts. there are many different options we could use and we are about to embark on a rewrite of the entire site, however until that time comes I am applyiong bandages to problems that I inherited. Thanks again for the help here.
# Posted By Jay McConathy | 5/8/07 9:28 AM