Skip to content

Commit 2a2a2c0

Browse files
committed
Dialog: Prevent dialog form losing focus (or move it back in IE <= 8).
1 parent 3829a37 commit 2a2a2c0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

tests/visual/dialog/complex-dialogs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
var datepickerDialog = $( "#dialog-datepicker" ).dialog({
3232
autoOpen: false,
33-
modal: true,
33+
modal: true
3434
}),
3535

3636
autocompleteDialog = $( "#dialog-autocomplete" ).dialog({

ui/jquery.ui.dialog.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ $.widget("ui.dialog", {
256256
this.uiDialog.hide();
257257
this._trigger( "close", event );
258258
}
259-
260-
return this;
261259
},
262260

263261
isOpen: function() {
@@ -305,6 +303,23 @@ $.widget("ui.dialog", {
305303
return this;
306304
},
307305

306+
_keepFocus: function( event ) {
307+
function checkFocus() {
308+
var activeElement = this.document[ 0 ].activeElement,
309+
isActive = this.uiDialog[ 0 ] === activeElement ||
310+
$.contains( this.uiDialog[ 0 ], activeElement );
311+
if ( !isActive ) {
312+
this.uiDialog.focus();
313+
}
314+
}
315+
event.preventDefault();
316+
checkFocus.call( this );
317+
// support: IE
318+
// IE <= 8 doesn't prevent moving focus even with event.preventDefault()
319+
// so we check again later
320+
this._delay( checkFocus );
321+
},
322+
308323
_createButtons: function( buttons ) {
309324
var that = this,
310325
hasButtons = false;
@@ -648,6 +663,10 @@ $.extend( $.ui.dialog.overlay, {
648663

649664
$el.appendTo( document.body );
650665

666+
$el.bind( "mousedown", function( event ) {
667+
dialog._keepFocus( event );
668+
});
669+
651670
if ( $.fn.bgiframe ) {
652671
$el.bgiframe();
653672
}

0 commit comments

Comments
 (0)