Solved

Does a modeless / modal dialog have an window.opener property

Posted on 2004-10-28
958 Views
Last Modified: 2008-01-09
Hi,

We are loading a modeless dialog where the user can enter some information.  We would like to write this back to it's parent before closing the window but if we try and specify window.opener etc we receive the error "Microsoft JScript runtime error: 'window.opener.document' is null or not an object".

Any thoughts on how a modeless dialog can refer back to it's parent to store a string?

Thanks.

James.
0
Question by:JAMES
    9 Comments
     
    LVL 17

    Accepted Solution

    by:
    <HTML>
     
    <script language=JavaScript>
     
           var winmodal;
         
            function ModalDialogMaintainFocus()
            {
                try
                {
                       if (winmodal.closed) { return; }
                       winmodal.focus();
                }
                catch (Exception) { }
            }
           
           
            function ModalDialogShow(BodyText,Buttons,ReturnValueElementName)
              {
                       var args='toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width=450,height=450';  

                             winmodal=window.open("","",args);
                             winmodal.document.open();
                             winmodal.document.write('<html>');
                             winmodal.document.write('<head>');
                             winmodal.document.write('<script' + ' language=JavaScript>');
                             winmodal.document.write('function CloseForm(Response) ');
                             winmodal.document.write('{ ');
                             winmodal.document.write('   var ret = window.opener.document.getElementById("' + ReturnValueElementName + '"); ');
                             winmodal.document.write('         ret.value = Response; ');
                             winmodal.document.write('         window.close(); ');
                             winmodal.document.write('} ');
                             winmodal.document.write('</script' + '>');        
                             winmodal.document.write('</head>');  
                             winmodal.document.write('<body onblur="window.focus();">');
                             winmodal.document.write('<table border=0 width="80%" align=center cellspacing=0 cellpadding=2>');
                             winmodal.document.write('<tr><td align=left>' + BodyText + '</td></tr>');
                             winmodal.document.write('<tr><td align=left><br></td></tr>');
                             winmodal.document.write('<tr><td align=center>' + Buttons + '</td></tr>');
                             winmodal.document.write('</body>');
                             winmodal.document.write('</html>');
                             winmodal.document.close();
                             winmodal.focus();
                             window.setInterval("ModalDialogMaintainFocus()",5);
     
              }

    </script>

    <script language=JavaScript>

              function YesNoCancel(ReturnValueElementName)
              {
                     var BodyText = '';
                     var Buttons='';
                     BodyText = 'Please enter yes, no, or cancel';
                     Buttons = '<a href=javascript:CloseForm("Yes");>Yes</a>  ';
                     Buttons += '<a href=javascript:CloseForm("No");>No</a>  ';
                     Buttons += '<a href=javascript:CloseForm("Cancel");>Cancel</a>  ';
                     ModalDialogShow(BodyText,Buttons,ReturnValueElementName);
              }

              function YesNoMaybe(ReturnValueElementName)
              {
                     var BodyText = '';
                     var Buttons='';
                     BodyText = 'Please enter yes, no, or maybe';
                     Buttons = '<a href=javascript:CloseForm("Yes");>Yes</a>  ';
                     Buttons += '<a href=javascript:CloseForm("No");>No</a>  ';
                     Buttons += '<a href=javascript:CloseForm("Maybe");>Maybe</a>  ';
                     ModalDialogShow(BodyText,Buttons,ReturnValueElementName);
              }

    </script>
     
    <BODY >

      <table border=1 cellpadding=2 cellspacing=2 align=center width="60%">
        <tr><td align=left></td></tr>
        <tr><td align=left></td></tr>
        <tr><td align=left></td></tr>
        <tr>
        <td align=left><a href="javascript:YesNoCancel('modalreturn1');">Show Modal #1</a>
           1. <input type=text id=modalreturn1 name=modalreturn1 value=''></td>
        </tr>
        <tr>
        <td align=left><a href="javascript:YesNoMaybe('modalreturn2');">Show Modal #2</a>
           2. <input type=text id=modalreturn2 name=modalreturn2 value=''></td>
        </tr>
     
      </table>

    </BODY>
    </HTML>

    Aeros
    0
     

    Author Comment

    by:JAMES
    Thanks Aeros,

    I saw this on my other post but havent had chance to look at it in detail yet.

    This does seem rather long-winded considering all im trying to do is pass back a string value! (I appreciate the whole lot may not apply to me but it's a lot to decipher).

    Do you know the answer to my title questions ie. "Does a modeless / modal dialog have an window.opener property"

    Thanks.

    James.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Window.showModalDialog is great in that it provides a truly modal dialog, but it has some serious limitations when wanting to create interactive dialogs. The best way to think of window.showModalDialog is that it is simply a dialog - it gathers values and can return a value. It does not have any "real" relationship to the calling window, except that it returns a single value to the calling code. It does not have a window.opener property, and as stated earlier, when called it does not provide a handle to the dialog window through the calling code - it simply provides a single, scalar return value once the box is closed. This can seriously mess up your code if you have interactive dialogs. Another problem I found with showModalDialog - any and all links in the dialog, including "javascript:function" links, launch into a new window - not in the dialog window. This is also undesired behavior. So, how can you get around this? Well, since we're already writing IE-specific code, let's go ahead and use another IE-specific call - iFrames.

    http://www.lotusgeek.com/SapphireOak/LotusGeekBlog.nsf/plinks/ROLR-5QT5TD
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    A very common misinterpretation is that the window.opener property should always be available as long as the creator window still exists. This is true providing the window.opener URL and the new window URL reside on the same domain, or at least on the same second-level domain. If the users navigate away from the domain with one of these windows, Internet Explorer enforces cross-frame scripting security.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    0
     

    Author Comment

    by:JAMES
    Both windows in my case are within the same domain - I will reread your example.

    A simple solution for me for the problem of ""javascript:function" links, launch into a new window " was to include "<base target="_self">" in the <HEAD> section of the child pop up and it then doesnt open a new window - although we are now going off-topic.

    James.
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    Dialogs, Modal/Modeless

    (note: 'Til Microsoft addresses these bugs, there's a free script include called ssDialogFix which fixes most of them.)
    document.title

    document.title can be dynamically set after a dialog's page load, but those changes are not reflected in the dialog window's title bar.
    window targeting

    Default window targeting is broken in dialogs. Instead of targeting the current window (_self) by default, it opens a new window. This problem applies to:

        * Hyperlinks (A tags)
        * Form submission
        * All programmatic interactions:
              o window.location = url
              o window.location.href = url
              o window.location.assign(url)
              o window.location.reload(url)
              o window.location.replace(url)
              o window.navigate(url)

    window.opener

    This property should point to the window object which opened the dialog.

    http://channel9.msdn.com/wiki/default.aspx/Channel9.InternetExplorerProgrammingBugs

    0
     
    LVL 15

    Expert Comment

    by:praneetha
    0
     
    LVL 17

    Expert Comment

    by:AerosSaga
    You should really use regular javascript though, developing with IE exclusive methods is not wise.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now