3

I am using jQuery and I am loading content into a div, then displaying it as a dialog. However, it is not centering the dialog.

Does anyone have a solution?

Code:

function Core_Load_Register()
{
    $("body").append("<div id='Register_Popup'></div>");
    $("#Register_Popup").load("index.php?section=FrontPage&page=Register&nogui=true");
    $("#Register_Popup").dialog({
        modal: true,
        height: "auto",
        width: "auto",
        buttons: 
        {
            "Register New Account":
            function() 
            {
                
            },
            
            "Cancel":
            function()
            {
                $(this).dialog("close");
            }
        }
    });
}

Example Screenshot:

jQuery Dialog not Centered Example

Community
  • 1
  • 1
Marcus Krueger
  • 163
  • 2
  • 11

3 Answers3

9

Since your content is dynamic try waiting for the load command to finish first.

$("#Register_Popup")
  .load("index.php?section=FrontPage&page=Register&nogui=true", 
  function()
  {
    $("#Register_Popup").dialog({ 
      modal: true, 
      height: "auto", 
      width: "auto", 
      buttons:  
      { 
        "Register New Account": 
        function()  
        { 

        }, 

        "Cancel": 
        function() 
        { 
            $(this).dialog("close"); 
        } 
      } 
  }); 
});
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
2

I found that using specific numeric values rather than the 'auto' height and width option works a lot better and always centers.

This isn't always optimal, especially when using dynamic data in the dialog.. but it worked for me (using jquery ui 1.9.2).

awbuboltz
  • 41
  • 3
0

My solution was similar to the accepted answer. Content was not dynamic in the sense of it being an AJAX call, rather the src of an image tag was assigned immediately before showing the dialog. Even with fixed width and height on the dialog it would fail to center.

setTimeout(function() {
            img.dialog({
                draggable: true,
                height: 'auto',
                width: 'auto',
                maxHeight: '768',
                maxWidth: '1024',
                modal: true
            });
        }, 500 );

By giving it a timeout it seems the dialog was able to be aware of its content and position itself correctly.

ficuscr
  • 6,975
  • 2
  • 32
  • 52