jQuery + ajax dialog + load external content

I was intrigued by Ray Camden's blog about simple dialog popups with jQuery. I saw some of his examples and wanted to take them a step further.

http://www.coldfusionjedi.com/index.cfm/2009/2/1/Creating-a-Dialog-with-jQuery-UI


His example shows that the content is inline with the document and the jQuery code just extracts it from the page and shows it in a dialog box. Cool, yeah, but...what if you wanted to pull in external content?

This is what I came up with:


$(document).ready(function(){
    //define config object
var dialogOpts = {
        title: "My First AJAX dialog",
        modal: true,
        autoOpen: false,
        height: 500,
        width: 500,
        open: function() {
        //display correct dialog content
        $("#example").load("beta/loremipsum.html");}
        };
$("#example").dialog(dialogOpts);    //end dialog
    
    $('#showdialog').click(
        function (){
            $("#example").dialog("open");
            return false;
        }
    );

});

The "example" div is within the page. It's blank. But, get's populated by the open option within the dialog options. That's how I got around it.

Can anyone tell me if this is the "best practices" way of doing this? Is there a better more efficient way?

Dialog Demo

Comments
Massimo Foti's Gravatar Personally, in such a scenario I prefer to load iframes inside the dialogs. It makes debugging much easier. See:
http://www.massimocorner.com/libraries/jquery/moda...
# Posted By Massimo Foti | 3/22/09 3:04 AM
Cary's Gravatar Another option is:
      $("#example").load('beta/loremipsum.html').dialog({
         title: "My First AJAX dialog",
modal: true,
autoOpen: false,
height: 500,
width: 500
      });
# Posted By Cary | 4/12/09 7:05 PM
Dada's Gravatar Great job!!

Is it possible to load an external webpage such as http://www.google.com ?? I've tried, but the box disappears. What changes would it need to load a url?

Thanks in advance!
# Posted By Dada | 4/14/09 3:53 AM
jorre's Gravatar yet another option

$(document).ready(function(){
var dialogOpts = {
      modal: true,
      bgiframe: true,
      autoOpen: false,
      height: 500,
      width: 500,
      draggable: true,
      resizeable: true,
   };
$("#example").dialog(dialogOpts);   //end dialog
   
   $('#showdialog').click(
      function() {
         $("#example").load("test.html", [], function(){
               $("#example").dialog("open");
            }
         );
         return false;
      }
   );
   
});
# Posted By jorre | 1/21/10 6:52 AM
Brian Lang's Gravatar Your original post worked exactly they way I needed it to. Thank you for posting!
# Posted By Brian Lang | 2/2/10 2:02 PM
Jason Buberel's Gravatar Thank you! This example (and the follow-ups) are exactly what I was looking for. Works like a charm!

-jason
# Posted By Jason Buberel | 2/16/10 8:39 PM
miro's Gravatar very good script. nice bat i have problem width download css images. Dialog I see widthout close icon on top right position.

Please help me width this

Thanks
# Posted By miro | 3/2/10 1:46 PM

Calendar

NAVIGATION

Recent Comments

RSS

Search

Subscribe

Tags