In the last post, Creating and Manipulating Modal Popups, I discussed what the JavaScript method showModalDialog is used for and how to implement it in your website.
Part two of this topic will focus on using popups to manipulate data and pass information from the child page back to the parent.
User Experience
Manipulating data in the popup window can be tricky, because modal windows do not function like normal ones. In a normal window, when your page does a postback, the postback is rendered in the same window as the original page. In a modal popup window, the postback opens a brand new window to re-render the popup. However, the original popup window does not close. In addition, this new window does not conform to the window feature settings defined in the parent page’s showModalDialog(), instead it opens based on the browser’s default for new windows.
Not good. Now your user is looking at two versions of the popup, the original (opened by the parent.htm page) and a second normal sized “non-modal” view of it (opened by the postback), which leaves the user with a poor experience of your website. To circumvent this catastrophe, you’ll want to use the <base> tag in the head of your popup page. Here is the syntax of the <base> tag:
<head> <base target=”_self” /> </head>This simple tag will ensure all postbacks of the child page are re-rendered within the same popup window object.
The Pass Back
Next, let’s examine the window.returnValue property, which is used to return a value or object back to the parent window. The showModalDialog method returns a value back to the parent window only when the window.returnValue property has been set in the popup.
Below is the parent.htm page which includes the javascript to open the child window and a textbox in the body which will be used to display data passed in from the child.htm page. Try it by copying the markup into a new page.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Modal Popup Example</title> <script type="text/javascript" language="javascript"> function openPopUpWithSize(theURL, title, heightInPX, widthInPX) { document.getElementById("MyParentTextbox").value = window.showModalDialog(theURL, title, 'dialogHeight=' + heightInPX + 'px;dialogWidth='+ widthInPX + 'px'); } </script> </head> <body style="text-align: center;"> Modal Popup Example <br/> Parent Page <br/> <br/> <input...