views:

248

answers:

3

In a dialog, I resize some images and then force the window to sizeToContent. Then, I want the dialog to center itself to the screen. How can I do this?

+1  A: 

The end result would be a window that moves itself? Please don't make it too annoying :)

Anyway, you'll have to do it manually using window.moveTo and various screen properties (see https://developer.mozilla.org/en/DOM/window)

Here's an interesting example, although it doesn't center the window, it ensures it's visible: http://www.koders.com/javascript/fid3F51B87DFD457428278627805CCA8D39ADC13455.aspx?s=window#L3

Nickolay
Excellent, exactly what I was looking for.
John Sheares
+1  A: 

A <dialog> element defines the moveToAlertPosition() and centerWindowOnScreen() convenience methods for you, and also copies them to the global scope so you don't have to scope them with document.documentElement.

Neil
A: 

I also searched around and looked to the MDC for anything which would center it but found nothing so I created this! This will work both on window and dialog.

var w=(screen.availWidth/2)-(document.getElementById('windowID').width/2);

var h=(screen.availHeight/2)-(document.getElementById('windowID').height/2);

window.moveTo(w,h);

The only thing you must change is windowID to the ID value of your window. It will work on all screen resolutions as it takes the total screen width and height then divides it in half thus giving the center of the screen then it subtracts your width and height settings to take them into account but divides them by half as well to offset the window as without the offset it will not be centered.

I hope this has helped!

Natalie Phox