views:

11

answers:

1

If I have a dialog and I want to center it on the screen, I can just do something like:

var w = window.openDialog('chrome://syndicus/content/saveDialog.xul', '', 'chrome,dialog');
w.centerWindowOnScreen();

but if it's a modal dialog, then the openDialog function doesn't return until after the user has pressed something, so centerWindowOnScreen won't get called until it's too late. How can I center a modal dialog?

+1  A: 

Put the call to centerWindowOnScreen in the JavaScript of the dialog itself:

<dialog xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        buttons="accept,cancel"
        ondialogaccept="onAccept();"
        ondialogcancel="onCancel();">
  <script type="text/javascript"><![CDATA[
    window.addEventListener('load', function() {
      window.centerWindowOnScreen();
    }, false);
    // more JavaScript ...
  </script>
</dialog>
MatrixFrog
This was not immediately obvious to me, so I thought I'd throw it out as a SO question. If anyone knows a better way than this, please feel free to post it.
MatrixFrog