views:

37

answers:

2

Hey All

Not quite sure how to do the following and hope you can advise.

I have a dialog which opens up a page through ajax. I would like to somehow once this dialog has finished loading trigger an event which then will perform some other action.

Can anyone advise on how to do such a task.

Hope you can help.

Lee

+1  A: 

Check out the jQuery API documentation for ajax. The "trigger" you need is called a callback function and either the "success", "error" or "complete" (used on either success or error) function is called when done:

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  },
  error: function(XMLHttpRequest, textStatus){
    alert(textStatus);
  }
});

I wasn't sure what dialog script you are using, are you using jQuery UI Dialog? And if the example above doesn't help, please provide the code you are trying to make work.

fudgey
He wants to be notified when the dialog has finished loading, not when the content has been received !
RobertPitt
Well I actually a seperate aJax complete which works great.ie,$('#dialog').ajaxComplete(function() { my_function();});Thnak you
Lee
+1  A: 
$( ".selector" ).dialog({
   open: function(event, ui)
   {
       //Dialog Open
   }
});

And

$( ".selector" ).bind( "dialogopen", function(event, ui)
{
  // Dialog Open
});

http://jqueryui.com/demos/dialog/#event-open


Update:

$.ajax(
{
    url: 'ajax/test.html',
    success: function(data)
    {
        var Contents = data;
        $dialog = $('<div></div>').attr('title',Contents.title).append($('<p></p>').val($Contents.contents));
        $($dialog).dialog({
           open: function(event, ui)
           {
                //Dialog Open
           }
        });
    }
});

as you defined Contents as a global scope its accessible in the open function of dialog.

RobertPitt
Hi Robert. I tried that but its not giving me the results I wanted. Its does bind to the open but not grabbing the contents.
Lee
updated with another example .
RobertPitt