Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto size a jQuery UI dialog in Internet Explorer

How can I autosize a jQuery UI dialog in Internet Explorer?

This code is OK in Firefox, but not in Internet Explorer.

$('#dialog2').dialog({
    autoResize: true,
    show: "clip",
    hide: "clip",
    height: 'auto',
    width: 'auto',
    autoOpen: false,
    modal: true,
    position: 'center',
    draggable: true,

    open: function (type, data) {
        $(this).parent().appendTo("form");

    },
    buttons: { "close": function () { $(this).dialog("close"); document.getElementById("<%=btnnew.ClientID%>").click(); } }
});

My HTML element is a DIV.

like image 348
Shahin Avatar asked Nov 24 '10 09:11

Shahin


1 Answers

I'm having success with width: 'auto' sizing jQuery UI dialog using the following "patch" (for IE) :

(function($) {
var fixDialogAutoWidth = $.noop;
if ( $.browser.msie ) {
    fixDialogAutoWidth = function(content) {
        var dialog = $(content).parent('.ui-dialog');
        var width = dialog.innerWidth();
        if ( width ) dialog.css('width', width);
    }
}

var _init = $.ui.dialog.prototype._init;
$.ui.dialog.prototype._init = function() {
    // IE magick: (width: 'auto' not working correctly) :
    // http://dev.jqueryui.com/ticket/4437
    if ( this.options.width == 'auto' ) {
        var open = this.options.open;
        this.options.open = function() {
            fixDialogAutoWidth(this);
            if ( open ) open.apply(this);
        }
    }
    // yet another bug options.hide: 'drop' does not work
    // in IE http://dev.jqueryui.com/ticket/5615
    if ( $.browser.msie && this.options.hide == 'drop' ) {
        this.options.hide = 'fold';
    }
    return _init.apply(this); // calls open() if autoOpen
};
})(jQuery);

Just load this code after jquery-ui.js has loaded ...

Note that according to the ticket http://dev.jqueryui.com/ticket/4437 we shouldn't be using width: 'auto' but I just couldn't live without it ... :)

like image 145
kares Avatar answered Nov 09 '22 14:11

kares