使模式对话框显示在IE窗口的中间
默认情况下,调用showModalDialog显示的模式对话框是在屏幕的中间,通过下面的代码可以使对话框显示在IE窗口的中央,无论IE窗口的大小和位置。
var wWidth=800;
var wHeight=600;
var wLeft = 0;
var wTop = 0;
function GetDialogParams(width, height)
{
var mLeft = 0;
var mTop = 0;
var wndDet = new GetWindowDetails();
mLeft = parseInt(((wndDet.wWidth - width)/2)+wndDet.wLeft)
mTop = parseInt(((wndDet.wHeight - height)/2)+wndDet.wTop)
return 'dialogHeight: '+height+'px; dialogWidth: '+width+'px; dialogTop: '+mTop+'px; dialogLeft: '+mLeft+'px; edge: Raised; center: No; resizable: no; status: no; location:no; scroll: no; help: no;'
}
function GetWindowDetails () {
WindowSize();
GetWindowLocation();
this.wWidth = wWidth;
this.wHeight = wHeight;
this.wLeft = wLeft;
this.wTop = wTop;
return this;
}
// Get Window Size
function WindowSize()
{
if( typeof( parent.window.innerWidth ) == 'number' ) {
//Non-IE
wWidth = parent.window.innerWidth;
wHeight = parent.window.innerHeight;
}
else if( parent.document.documentElement &&
(parent.document.documentElement.clientWidth ||
parent.document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
wWidth = parent.document.documentElement.clientWidth;
wHeight = parent.document.documentElement.clientHeight;
}
else if( parent.document.body &&
(parent.document.body.clientWidth ||
parent.document.body.clientHeight ) ) {
//IE 4 compatible
wWidth = parent.document.body.clientWidth;
wHeight = parent.document.body.clientHeight;
}
}
// Get Window Location
function GetWindowLocation () {
if (!document.layers && document.all) {
wLeft = parent.window.screenLeft;
wTop = parent.window.screenTop;
}
else {
wLeft = parent.window.screenX;
wTop = parent.window.screenY;
}
}
var wHeight=600;
var wLeft = 0;
var wTop = 0;
function GetDialogParams(width, height)
{
var mLeft = 0;
var mTop = 0;
var wndDet = new GetWindowDetails();
mLeft = parseInt(((wndDet.wWidth - width)/2)+wndDet.wLeft)
mTop = parseInt(((wndDet.wHeight - height)/2)+wndDet.wTop)
return 'dialogHeight: '+height+'px; dialogWidth: '+width+'px; dialogTop: '+mTop+'px; dialogLeft: '+mLeft+'px; edge: Raised; center: No; resizable: no; status: no; location:no; scroll: no; help: no;'
}
function GetWindowDetails () {
WindowSize();
GetWindowLocation();
this.wWidth = wWidth;
this.wHeight = wHeight;
this.wLeft = wLeft;
this.wTop = wTop;
return this;
}
// Get Window Size
function WindowSize()
{
if( typeof( parent.window.innerWidth ) == 'number' ) {
//Non-IE
wWidth = parent.window.innerWidth;
wHeight = parent.window.innerHeight;
}
else if( parent.document.documentElement &&
(parent.document.documentElement.clientWidth ||
parent.document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
wWidth = parent.document.documentElement.clientWidth;
wHeight = parent.document.documentElement.clientHeight;
}
else if( parent.document.body &&
(parent.document.body.clientWidth ||
parent.document.body.clientHeight ) ) {
//IE 4 compatible
wWidth = parent.document.body.clientWidth;
wHeight = parent.document.body.clientHeight;
}
}
// Get Window Location
function GetWindowLocation () {
if (!document.layers && document.all) {
wLeft = parent.window.screenLeft;
wTop = parent.window.screenTop;
}
else {
wLeft = parent.window.screenX;
wTop = parent.window.screenY;
}
}
使用方法如下:
var params = GetDialogParams(200, 100);
var result = window.showModalDialog('url', '', params);
var result = window.showModalDialog('url', '', params);