Skip to content

Instantly share code, notes, and snippets.

@DouglasMeyer
Created January 15, 2013 15:58
Show Gist options
  • Save DouglasMeyer/4539663 to your computer and use it in GitHub Desktop.
Save DouglasMeyer/4539663 to your computer and use it in GitHub Desktop.
Some dialog behavior. Would it be better for the dialog to be an iframe? Depends...
var dialog = $('<div />').appendTo(document.body);
// Close the dialog when the user clickes out of it.
$('body').on('mousedown', function(){ dialog.remove(); });
dialog.on('mousedown', function(e){
// Don't let clicks bouble-up to the body and remove the dialog.
e.stopPropagation();
});
// Open links in the dialog
dialog.on('click', 'a', function(e){
e.preventDefault();
dialog.load(this.href);
});
// Submit forms within the dialog (Not complete, or probably safe).
dialog.on('click', 'form input[type="submit"]', function(e){
e.preventDefault();
var form = $(this).closest('form');
$.ajax({
type: 'POST',
url: form.attr('action'),
data: form.serialize(),
complete: function(xhr, status){
dialog.html(xhr.responseText);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment