Can't call window.parent from IE7?

I am trying to make a simple call to the parent class from a modal popup, but IE fights me all the time. Any suggestions to get around it would be greatly appreciated.

Below is the remote code that I am trying to use. It works fine in FireFox, but causes an error in IE - "Object does not support this property or method", referring to a line of code in the "Catch" block. This means that both lines in the Try and Catch blocks do not work.

parent.html

<html><head> <script> function callMain(msg) { alert(msg); } function modalWin() { if (window.showModalDialog) { window.showModalDialog("topFrame1.html","name", "dialogWidth:255px;dialogHeight:250px"); } else { window.open('topFrame1.html','name', 'toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes'); } } function getMainFrameVal() { return document.getElementById("mainframe").value; } </script> </head> <body> <a href="#" onclick="modalWin()" >PopUpWindow</a> <form> <input type=text id="mainframe" value="main"/> </body></html> 

topFrame1.html

 <html><head> <script type="text/javascript"> function getMain(){ try{ alert("1 "+ window.opener.getMainFrameVal()); }catch(e) { alert("2 " +window.parent.getMainFrameVal()); } } </script> </head> <body> TOP <a href="#" onclick="getMain()">click for main</a> <br/><br/> </body></html> 
+4
source share
2 answers

window.opener designed to open popups with window.open() .

Are you sure parent.html is the parent element of topFrame1.html and you are not secretly using a frameset or something else?

0
source

The modified IE dialog is not really a true window and does not support window.opener. To refer to the parent window, you will need to pass the link to the window as part of the dialog arguments, for example:

  window.showModalDialog("topFrame1.html",["name", window], "dialogWidth:255px;dialogHeight:250px"); 

Then you can refer to the parent in the child with this line:

  alert("1 "+ window.dialogArguments[1].getMainFrameVal()); 

My advice is to avoid IE dialogs altogether and just use one solution that works in all browsers like window.open() or jQuery dialogs .

0
source

Source: https://habr.com/ru/post/1383534/


All Articles