[WEB4LIB] Can JavaScript or ASP be used to adjust a window's size to fit

Thomas Dowling tdowling at ohiolink.edu
Tue Jan 15 11:19:50 EST 2002


>At 10:28 AM 1/14/2002, Info Galway Library wrote:
>
>I am using JavaScript to open a small window to display a document. The
>document is not large? I want the window to be sufficiently large to contain
>the document; no bigger or no smaller. Is there any means of opening a
>window of the required size? I am aware the document will be of a different
>size in different browsers.

If you realize that the document will display at different sizes, not just 
in different versions of browsers, but in identical browsers with different 
user settings, then you're way ahead of too many web designers.  However, 
it also follows that you can't very reliably say "the document is not 
large" (if you're talking about physical dimensions).  You just don't know.

>I am familiar with ASP and JavaScript.


You should be aware that none of the following can be *forced* on the 
user.  In Opera 6 (and Mozilla, soon, we hope), there's a user setting to 
disable pop-up windows, and that's without even disabling client-side 
scripting.  Why?  Because many users really don't like them.  You're sure 
this is a good idea, right?

If you're going to do this, it has to happen on the client side, so you'll 
want to use JavaScript.  You're familiar with JavaScript, so you already 
know that window.open uses pixel-based dimensions to size the new 
window.  You could just stick with static pixel dimensions, but I'll tell 
you from experience that there's no good window size that works on 
everything from 640x480 to 1600x1200 resolutions.

You can (for many Javascript-enabled browsers) get the user's screen 
resolution with screen.width and screen.height and make a reasonable guess 
about what window size to use.  The following could be used to open a 
window that covers about a quarter of the height and width of the 
screen.  That will fit the same amount of content from screen to screen 
more closely than static pixel sizes.

   //any errors are included as an exercise for the reader :-)
   if (screen.width) {
     ourWidth = Math.round(screen.width * .25);
   } else {
     ourWidth = 250;
   }
   if (screen.height) {
     ourHeight = Math.round(screen.height * .25);
   } else {
     ourHeight = 200;
   }
   ourSettings = "menubar=yes,scrollbars=yes,toolbar=yes,status=yes,";
   ourSetings = ourSettings + "directories=yes,location=yes,resizable=yes,"
   ourSettings = ourSettings + "width=" + ourWidth + ",";
   ourSettings = ourSetting + "height=", ourHeight;
   theNewWin = window.open("SomeURL", "SomeWindowName", ourSettings);
   theNewWin.focus();

A couple of general notes.  First, opening a new window for any purpose is 
a pretty drastic measure.  We do it in our research databases, but only for 
features that have the effect of taking users entirely out of the search 
process.  Second, you have to be kind to your non-Javascript 
browsers--there are more of us than you may know--so your hyperlink has to 
work with and without JS.  With a little tweaking to the script above, this 
will do the trick.

   <a href="read-this-right-now.html"
     target="VitallyImportantWindow"
     onclick="runTheScript('read-this-right-now.html', 
'VitallyImportantWindow')">




More information about the Web4lib mailing list