20

I am opening a modal popup window. Then I access a parent window textbox and other attributes using window.opener. It is working fine in firefox but not in IE8. It gives error 'window.opener is null'. How can I access parent window attributes in child window which works in both browsers.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
dmay
  • 1,305
  • 6
  • 21
  • 31
  • Have you tried window.parent.opener? – Shakti Singh Feb 03 '11 at 11:52
  • i try it window.parent.opener but i unable to access parent document object. I pass parent.document as argument and access parent.document in popup as window.dialogArguments.parentDocumentObj where parentDocumentObj is name of variable which contains document. – dmay Feb 16 '11 at 12:04

5 Answers5

10

There are two ways to solve the problem: Note: "window.opener" is not supported by IE if "showModalDialog" is been used.

1) Instead of "window.showModalDialog" use "window.open"

2) If you want to use "window.showModalDialog" then do the following:

<script language="javascript" type="text/javascript">
    function YourFunction()
    {
        var opener = null;

        if (window.dialogArguments) // Internet Explorer supports window.dialogArguments
        { 
            opener = window.dialogArguments;
        } 
        else // Firefox, Safari, Google Chrome and Opera supports window.opener
        {        
            if (window.opener) 
            {
                opener = window.opener;
            }
        }       
        // write you code and refer "opener"
        window.close();
    }
</script>
Alan
  • 45,915
  • 17
  • 113
  • 134
Krishn Y
  • 111
  • 1
  • 2
8

You can pass arguments to showModalDialog function. Simply pass window object as an argument.

window.showModalDialog(theURL, window);

Yo can access the arguments from the modal window using dialogArguments. See: http://msdn.microsoft.com/en-us/library/ms533723%28VS.85%29.aspx

var openerWindow = window.dialogArguments;
Kaitnieks
  • 922
  • 1
  • 8
  • 15
  • I should probably note that this works in IE and FF and probably other browsers, too. – Kaitnieks Feb 03 '11 at 13:55
  • 3
    showModalDialog is deprecated by chromium 35 (July 2014). See http://blog.chromium.org/2014/07/disabling-showmodaldialog.html and http://blog.chromium.org/2014/04/chrome-35-beta-more-developer-control.html – edigu Jan 13 '15 at 10:36
1

Disable Internet Explorer's "Protected Mode", which prevents access to this object.

The steps for this are:

  1. Press Alt+T to show the Tools menu
  2. Click "Internet options"
  3. Select the "Security" tab
  4. Make sure zone selected contains your site. For an intranet site it would typically be "Local intranet" zone.
  5. Untick "Enable Protected Mode"
  6. Close all IE tabs and windows and re-open.

Now you should be able to access the window.opener object.

Aidan
  • 313
  • 1
  • 6
0

The approach I would take is the following:

  1. Use an existing JavaScript UI library because you are not the first person to ever want to do this, failing that
  2. Create a function called OpenWindow, that browser sniffs for the window.opener method

For example:

if(window.opener == undefined) {
   //probably not Firefox...
}

and if it finds it then uses it, else it tests for the IE variant and uses it. And then it checks Safari's version, etc...

Smi
  • 13,850
  • 9
  • 56
  • 64
Xhalent
  • 3,914
  • 22
  • 21
0

As a cross-browser alternative, you can give a custom attribute to the new window while you are opening it:

var popup = window.open(...);
popup.isPopup = true;

Then, in the referred page:

if (window.isPopup) {
  // Do something
}
else {
  // Not in a popup
}
Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55