167 out of 332 rated this helpful Rate this topic

showModalDialog Method

.NET Framework 3.0

Creates a modal dialog box that displays the specified HTML document.

Syntax

vReturnValue = object.showModalDialog(sURL [, vArguments] [, sFeatures])

Parameters

sURL Required. String that specifies the URL of the document to load and display.
vArguments Optional. Variant that specifies the arguments to use when displaying the document. Use this parameter to pass a value of any type, including an array of values. The dialog box can extract the values passed by the caller from the dialogArguments property of the window object.
sFeatures Optional. String that specifies the window ornaments for the dialog box, using one or more of the following semicolon-delimited values:
dialogHeight:sHeight
Sets the height of the dialog window (see Remarks for default unit of measure).
dialogLeft:sXPos
Sets the left position of the dialog window relative to the upper-left corner of the desktop.
dialogTop:sYPos
Sets the top position of the dialog window relative to the upper-left corner of the desktop.
dialogWidth:sWidth
Sets the width of the dialog window (see Remarks for default unit of measure).
center:{ yes | no | 1 | 0 | on | off }
Specifies whether to center the dialog window within the desktop. The default is yes.
dialogHide:{ yes | no | 1 | 0 | on | off }
Specifies whether the dialog window is hidden when printing or using print preview. This feature is only available when a dialog box is opened from a trusted application. The default is no.
edge:{ sunken | raised }
Specifies the edge style of the dialog window. The default is raised.
resizable:{ yes | no | 1 | 0 | on | off }
Specifies whether the dialog window has fixed dimensions. The default is no.
scroll:{ yes | no | 1 | 0 | on | off }
Specifies whether the dialog window displays scrollbars. The default is yes.
status:{ yes | no | 1 | 0 | on | off }
Specifies whether the dialog window displays a status bar. The default is yes for untrusted dialog windows and no for trusted dialog windows.
unadorned:{ yes | no | 1 | 0 | on | off }
Specifies whether the dialog window displays the border window chrome. This feature is only available when a dialog box is opened from a trusted application. The default is no.

Return Value

Variant. Returns the value of the returnValue property as set by the window of the document specified in sURL.

Remarks

A modal dialog box retains the input focus while open. The user cannot switch windows until the dialog box is closed.

Because a modal dialog box can include a URL to a resource in a different domain, do not pass information through the vArguments parameter that the user might consider private. The vArguments parameter can be referenced within the modal dialog box using the dialogArguments property of the window object. If the vArguments parameter is defined as a string, the maximum string length that can be passed to the modal dialog box is 4096 characters; longer strings are truncated.

You can set the default font settings the same way you set Cascading Style Sheets (CSS) attributes (for example, "font:3;font-size:4"). To define multiple font values, use multiple font attributes.

To override center, even though the default for center is yes, you can specify either dialogLeft and/or dialogTop.

When Windows Internet Explorer opens a window from a modal or modeless HTML dialog box by using the showModalDialog method or by using the showModelessDialog method, Internet Explorer uses Component Object Model (COM) to create a new instance of the window. Typically, the window is opened by using the first instance of an existing Internet Explorer process. When Internet Explorer opens the window in a new process, all the memory cookies are no longer available, including the session ID. This process is different from the process that Internet Explorer uses to open a new window by using the open method.

For Internet Explorer 7, dialogHeight and dialogWidth return the height and width of the content area and no longer includes the height and width of the frame.

Internet Explorer 7. Although a user can manually adjust the height of a dialog box to a smaller value —provided the dialog box is resizable— the minimum dialogHeight you can specify is 100 pixels, and the minimum dialogWidth you can define is 250 pixels. In versions earlier than Internet Explorer 7 the minimum value of the dialogWidth that can be specified is 100 pixels.

Note  For Internet Explorer 7, help is not a valid value for sFeatures. In previous versions, help:{ yes | no | 1 | 0 | on | off } specified whether the dialog window displays the context-sensitive Help icon.

This method must use a user-initiated action, such as clicking on a link or tabbing to a link and pressing enter, to open a pop-up window. The Pop-up Blocker feature in Microsoft Internet Explorer 6 blocks windows that are opened without being initiated by the user.

Internet Explorer 5 and later allows further control over modal dialog boxes through the status and resizable values in the sFeatures parameter of the showModalDialog method. Turn off the status bar by calling the dialog box from a trusted application, such as Microsoft Visual Basic or an HTML Application (HTA), or from a trusted window, such as a trusted modal dialog box. These applications are considered to be trusted because they use Internet Explorer interfaces instead of the browser. Any dialog box generated from a trusted source has the status bar turned off by default. Resizing is turned off by default, but you can turn it on by specifying resizable=yes in the sFeatures string of the showModalDialog method.

The default unit of measure for dialogHeight and dialogWidth in Internet Explorer 5 and later is the pixel. The value can be an integer or floating-point number, followed by an absolute units designator ( cm, mm, in, pt, or pc) or a relative units designator ( em, ex, or px). For consistent results, specify the dialogHeight and dialogWidth in pixels when designing modal dialog boxes.

As of Internet Explorer 4.0, you can eliminate scroll bars on dialog boxes. To turn off the scroll bar, set the SCROLL attribute to false in the body element for the dialog window, or call the modal dialog box from a trusted application.

Examples

This example uses the showModalDialog method to open a customized dialog box.


<script type="text/javascript">
function fnRandom(iModifier){
   return parseInt(Math.random()*iModifier);
}
function fnSetValues(){
   var iHeight=oForm.oHeight.options[
      oForm.oHeight.selectedIndex].text;
   if(iHeight.indexOf("Random")>-1){
      iHeight=fnRandom(document.body.clientHeight);
   }
   var sFeatures="dialogHeight: " + iHeight + "px;";
   return sFeatures;
}
function fnOpen(){
   var sFeatures=fnSetValues();
   window.showModalDialog("showModalDialog_target.htm", "", 
      sFeatures)
}
</script>
<form name="oForm">
	Dialog Height 
	<select name="oHeight">
		<option>-- Random --</option>
		<option>150</option>
		<option>200</option>
		<option>250</option>
		<option>300</option>
	</select> 
	Create Modal Dialog Box
	<input type="button" value="Push To Create" onclick="fnOpen()">
</form>

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog2.htm

Linked from the following button is an extended version of the above script, which demonstrates many of the features available for creating a custom dialog box.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog.htm

Neither modal nor modeless HTML dialog boxes support text selection or the standard shortcut menu for copy operations; however, you can imitate this functionality by using script with TextRange objects and event handlers for onmousedown and onmousemove, as in the following example.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialogLaunch.htm

Standards Information

There is no public standard that applies to this method.

Applies To

window, Window Constructor

See Also

Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
showModalDialog Method for existing form
How would I use this method to pass data from a modal dialog to an existing text area? The purpose is to add to or edit the existing text area from the data input in the modal dialog, similar to the jquery ui modal dialog. The reason why I am not using jquery ui modal dialog is that it is not working well with an existing form's textarea and in addition to other issues. Thanks in advance for your help.
IE versions support
Hi All,
This sounds very useful to me...

  1. Which IE versions does it support ? Any known Browser issues ?
  2. I want to construct my own confirm box with custom buttons. Will this help ?
  3. Does it have any reference to the Parent window from where it was called  and vice a versa ?

Thanks,
Piyush
IE9: Selected text no longer highlighted
I implemented a search and replace feature in an asp web application. The user can select text on a page and click ctrl+F. When they do, a custom modal dialog is shown. They can enter search text and click [Find]. The web page is searched, the found text is highlighted using the select() method, and the custom modal dialog is redisplayed. Prior to IE9, when the modal dialog was redisplayed, the selected text was still highlighted and visible in the parent window behind the dialog. Now, in IE9, as soon as the modal dialog is displayed, the highlighting disappears from the text in the parent window and they cannot see where the text they were searching for was found.

Does anybody know if there is a way to keep the selected text highlighted in the parent window when a modal dialog is displayed?
Popup behavior in IE9.
I'm working on an application that uses the window.showModalDialog function to open popups and display other pages of the app. The issue is that in IE 9 and above the popups open as seperate windows instead of a child window of the parent page. I need these window to show as part of the parent window on the task bar.

This is a sample of a javascript function I've used -

function ShowdlgBDC_PrintDates() {
window.showModalDialog('<%=Page.ResolveUrl("~/Common/Pbdc_dlgBDC_PrintDates.aspx")%>', 'Pbdc_dlgBDC_PrintDates', 
'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no', 
'dependent=yes, menubar=no, toolbar=no,status=no,modal=yes,scroll=no');
}

This works with the expected behavior in IE 6 and below. It opens up as a popup but it does not show up as a new window on the task bar. I've tried the following with no success -

- Setting the compatibility view to IE 7 (both the browser and the document modes) using the development bar and compatibility view options.
- Changing the tabbed browsing settings to open both popups and links from other programs in a new tab of the current window
- Setting the name parameter of the showModalDialog to '_parent' , '_blank' and '_top'.
- Using the <base target="_self"> node both within and out side the head nodes of the parent and the child pages. I've also tried placing this only in the child page.

How do I get the child page to show up without appearing as a new window on the taskbar using IE9? Any help would be appreciated. Thanks.
IE9 Pinned Sites - Cookie Absent
$0IE9's enhanced security for pinned sites causes IE dialogs with hyperlinks to open their windows without the supplying the cookie and this forces the user to re-authenticate.  $0
A blank dialog when url is invalid
When we open normal window page with invalid URL, the page shows the message stating that 'The page can not be displayed' along with related description. This does not happen with Modal Dialog. In my application there is one use case that I need to handle.  After successfully launching an HTML page, the server went down. There is a link for modal dialog pop up in the page. When I launched the modal dialog, a blank dialog get opened. There is no way for end user to get why it is blank.  Is there any way to handle such case or the issue is by design. If it is by design, what is the significance of it?
On IE8, AddFavorite fails from this new modal window.
My Modal window has a button "Add To Favorites". When the user clicks on it, browser Add To favorite should open. It is not opening. It works in IE7/6 but not in IE8. No error is seen. $0Code for modal window:$0 $0 $0$0 $0 $0
<html>
<body>
<SCRIPT TYPE="text/javascript">
function addToFav()
{
var url = "http://localhost:8080/abc";
var decodedTitle = "Test1";
    window.dialogArguments.external.AddFavorite(url, decodedTitle);
}
</SCRIPT>
     <FORM>
<TABLE BORDER CELLPADDING=3>
<TR>

<TD><INPUT TYPE=BUTTON OnClick="addToFav();" VALUE="Add To Fav"></TD>
</TR>
</TABLE>
</FORM>
   </body>
</html>
$0
$0This modal window has been created from another html using this:$0 $0 $0$0 $0 $0
 var strFeatures = "dialogWidth:" + 250 + "px;" + "dialogHeight:" + 250 + "px;" +
                         "dialogLeft:" + 10 + "px;" + "dialogTop:" + 10 + "px;" +
                         "status:yes;resizable:yes;scroll:no;";

var retVal = window.showModalDialog(strUrl, window, strFeatures);
$0
$0$0 $0
Strange behaviour throught https protocol
if I open dialog like this $0var oArgs = {}; $0 $0oArgs.opener = this; $0 $0oArgs.param = 'Тест'; $0 $0window.showModalDialog("http://nysite/Dialog.html", oArgs, "dialogWidth:100px;dialogHeight100px:help:0;status:0;scroll:0;center:1");$0 $0$0 $0everything is ok -I can get dialogArguments - var test = window.dialogArguments.param;. If I open dialog throught https - window.showModalDialog("https://nysite/Dialog.html", oArgs, "dialogWidth:100px;dialogHeight100px:help:0;status:0;scroll:0;center:1");  dialogArguments is null - why???$0
Javascript To operate ShowModal Dialog window
Hi All, 
I want to identify and perform operation on any existing show-modal dialog box using Javascript. Is there any way to do that.
It's a Dialog, not a Window
Now that Firefox supports showModalDialog, it is a tempting replacement to window.open().  But there are several differences.

As noted below, a form inside a ModalDialog will open in a new window, unless you use <base target='_self'> in the <head> section.  Do not use target in the <form> tag; that won't work.  The <a> (anchor) tag has the same behavior (and can be fixed the same way).  The window.location() JScript (javascript) function also opens a new window, and the <base> tag does not change that.  I do not know of a work-around (other than programmatically setting a hidden <input> field in a <form>, and then invoking the submit method of the form.)

The window.opener method returns null, rather than a reference to the opening window.  So you cannot refresh the opening window  with window.opener.location.refresh() (if, for instance, you use showModalDialog to open an editing dialog).  If all you want to do is refresh the opening window every time the ModalDialog closes, that is easy (include window.location.refresh() right after the call to showModalDialog).  But if you only want to refresh the opening window in certain cases (e.g., the opening window takes a while to refresh), you can do that by passing a dialogArgument.  

A more clever (I think) way is to pass the window reference itself as the dialogArgument.  In the calling window, use window.showModalDialog('newurl.asp',window).  In the called dialog retrieve the reference with var window_opener = window.dialogArguments.  You can use the window reference stored in variable window_opener in place of window.opener, to refresh the calling window from the called dialog.

Do note that Firefox and Chrome (for instance) do not appear to have these limitations, and appear to treat ModalDialogs more like regular windows.  Keep that in mind if you do testing using one of these browsers, but intend your application to work in all browsers.
Limitations
This method throws a run-time error for context menu windows, scriptlets and the like, and when an Internet zone window tries to show a local window. As a workaround, you can overlay an HTML element onto the target window or use a resource page.
window.onbeforeunload or body onUnload events are not working on modal dialog
Events like window.onbeforeunload or body onUnload are not working for modal dialog. Can anybody help to call javascript while closing dialogbox? Or can we remove close button from dialog box?
Form Submission Behavior

When an HTML form is submitted within a ModalDialog window, the behaviour is different (opens in a new window). To workaround this issue, the page that will be displayed inside the modal window should have the following tag added inside the HEAD node of the document's HTML:

<base target="_self"> 


text resize is not happening thru Crtl+ mouse scroll, when showmodaldialog method is used.
When showmodaldialog method is used to create popup window, text resize is not happening thru Crtl+ mouse scroll.

I need help to resize the font size in the popup window.

tia

[tfl - 05 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Find doesn't work in modal dialogs
When you press Ctrl+F in a modal dialog nothing happens. Why?

[tfl - 05 10 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
PowerShell : http://groups.google.com/group/microsoft.public.windows.powershell/topics?pli=1
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
Some useful tips and tricks
http://blogs.msdn.com/dditweb/archive/2006/02/20/535734.aspx
Status=0 does not work from Win XP SP2 forward.
If IE is on a Windows XP machine with at least SP2, the browser will not allow you to set the status=0 and hide the status bar. The status bar will always show. If the browser's security setting under 'Miscellaneous: Allow Script initiated issues without size or position constraints' is set to 'enabled', the status bar will not show.
Copy Content
How can i copy the content of a ShowModal window in HTML? It should only allows me to copy not to paste or edit.

[jsudds.MSFT] Modal and modeless HTML dialogs do not support text selection and copy; however, you can imitate this functinality with a bit of script. Check out a working sample here:

http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialogLaunch.htm
How to set the window title of a modal dialog box?

I can't see the window title, but my page have the setting code like this: "<title>Page Title</title>"

[jsudds.MSFT] The dialog title is inherited from the parent page (as you might have noticed). However, you can change the dialog title after the page has loaded as follows:

window.onload = function() {
    document.title = "New Dialog Title";
}
Using ShowModal in MS CRM 4.0 ISV Config

I have a custom entity that uses an aspx page called using showmodal, I need to be able to both close the modal dialog cleanly and also to refresh data on the parent form after some data manipulation has taken place.

I have used <base target="_self"/> in the head tag and this allows me to cleanly close the modal dialog. The problem I have is that since implementing this tag I can no longer refresh the form of the custom entity that launches the modal dialog using 'window.opener.location.reload();'

How to remove close button & question mark button from modal Dialog box

I am unable to remove close button & question mark button from modal Dialog box. Please Guide me in this regard. I urgently need the answer for this query

OR

Just let me know how to remove close, restore & minimize button from popup.

Modal window or modal tab?

I am able to open the modal window with showModalWindow() but it is modal to all the tabs. Can I make it modal to the same tab only?

Modal Submission Fix... And: Modal print preview question

<QUOTE> When an HTML form is submitted within a ModalDialog window, the behaviour is different (opens in a new window). This could have also been specified in this article with workaround or solution. </QUOTE>

.

There IS a workaround to that behaviour: Add <base target="_self"/> inside of the <head> tags in the .aspx file of the ModalDialog window.

One question, though: Any way to fire-up a print preview dialog from ModalDialog? Turn it to modeless -> print preview -> turn it to modal again?

Embedded IE
For embedding IE in applications, note that this method will not fire a NewWindow2 event. (KB251128)