Hello, I've been trying to find something about why window.opener doesn't work for IE (I'm using 6.0) when I use the window.opener in a separate JavaScript file. It works without any problem when I use Mozilla/Netscape, but not for IE.

The implementations I've seen in my searching for an answer so far, have all been 'in-line' examples how to manage but I haven't found any example yet with separate JavaScript file.

I've got two XHTML documents that are connected to two different JavaScript files:

The first XHTML document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http&#58;//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http&#58;//www.w3.org/1999/xhtml" xml&#58;lang="en">
   <head>
      <title>Test first window</title>
      <meta http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1" />
   </head>
   <body>
      <h2>Test first window</h2>
      <hr />
      <div class="left">
         <p>
            Open a new browser window&#58;
            <input id="openNewWindow" type="text" size="55"
               value="commsecondwindow.html" />
            <input id="button1" type="button" value="Open" />
         </p>
         <p>
            Close the browser window&#58;&nbsp;&nbsp;&nbsp;
            <input id="button2" type="button" value="Close" />
         </p>
         <p id="textString">
         </p>
         <p id="timeString">
         </p>
         <p id="testString">
         </p>
      </div>
      <script type="text/javascript" src="commfirstwindow.js"></script>
      <noscript><p>Your browser does not support javascript.</p></noscript>
   </body>
</html>

...and the javascript for it:

// For IE4+
ie=&#40;document.all&#41;?true&#58;false;

// For Mozilla
dom=&#40;&#40;document.getElementById&#41; && &#40;!ie&#41;&#41;?true&#58;false;

function setEventByID&#40;id, ev, fu&#41; &#123;
   if &#40;dom&#41; &#123;
      document.getElementById&#40;id&#41;.addEventListener&#40;ev, fu, false&#41;;
   &#125;
   if &#40;ie&#41; &#123;
      document.getElementById&#40;id&#41;.attachEvent&#40;'on' + ev, fu&#41;;
   &#125;
&#125;



var newWindow;

function openNewWindow&#40;&#41; &#123;
   var url=document.getElementById&#40;'openNewWindow'&#41;.value;
   newWindow=window.open&#40;url, 'newWindow', '', ''&#41;;
   newWindow.creator=self;
&#125;
setEventByID&#40;'button1', 'click', openNewWindow&#41;;


// Close the new window

function closeWindow&#40;&#41; &#123;
   if &#40;newWindow!=null&#41; &#123;
      newWindow.close&#40;&#41;;
   &#125;
&#125;
setEventByID&#40;'button2', 'click', closeWindow&#41;;

The second XHTML document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http&#58;//www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http&#58;//www.w3.org/1999/xhtml" xml&#58;lang="en">
   <head>
      <title>Test second window</title>
      <meta http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1" />
   </head>
   <body>
      <h2>Test second window</h2>
      <hr />
      <div class="left">
         <p>
            Send a text to the main window&#58;
            <input id="testText" type="text" size="55"
               value="This is a test text for the main window!" />
            <input id="button3" type="button" value="Send text" />
         </p>
      </div>
      <script type="text/javascript" src="commsecondwindow.js"></script>
      <noscript><p>Your browser does not support javascript.</p></noscript>
   </body>
</html>

...and the javascript code:

// For IE4+
ie=&#40;document.all&#41;?true&#58;false;

// For Mozilla
dom=&#40;&#40;document.getElementById&#41; && &#40;!ie&#41;&#41;?true&#58;false;

function setEventById&#40;id, ev, fu&#41; &#123;
   if &#40;dom&#41; &#123;
      document.getElementById&#40;id&#41;.addEventListener&#40;ev, fu, false&#41;;
   &#125;
   if &#40;ie&#41; &#123;
      document.getElementById&#40;id&#41;.attachEvent&#40;'on' + ev, fu&#41;;
   &#125;
&#125;

// Global variables
var initialString="Testing to insert text into main window...";
var initialTextNode=document.createTextNode&#40;initialString&#41;;
var timeNode;
var testNode;


window.opener.document.getElementById&#40;'textString'&#41;.appendChild&#40;initialTextNode&#41;;


function currentTime&#40;&#41; &#123;
   var today=new Date&#40;&#41;;
   if &#40;timeNode==null&#41; &#123;
      timeNode=document.createTextNode&#40;today.toLocaleTimeString&#40;&#41;&#41;;
      window.opener.document.getElementById&#40;'timeString'&#41;.appendChild&#40;timeNode&#41;;
   &#125;
   else &#123;
      window.opener.document.getElementById&#40;'timeString'&#41;.removeChild&#40;timeNode&#41;;
      timeNode=document.createTextNode&#40;today.toLocaleTimeString&#40;&#41;&#41;;
      window.opener.document.getElementById&#40;'timeString'&#41;.appendChild&#40;timeNode&#41;;
   &#125;
&#125;
window.setInterval&#40;currentTime, 1000&#41;;

function sendTestText&#40;&#41; &#123;
   var testTheString=document.getElementById&#40;'testText'&#41;.value;
   testNode=document.createTextNode&#40;testTheString&#41;;
   window.opener.document.getElementById&#40;'testString'&#41;.appendChild&#40;testNode&#41;;
&#125;
setEventById&#40;'button3', 'click', sendTestText&#41;;

Does anyone know why the above works well with Mozilla/Netscape but doesn't work with IE? Am I doing something wrong in the code?

/Soo-Im

Hello, I'll answer my own question...

There was definitely something wrong with my code that I was not aware of until now. When creating the nodes, I also have to use window.opener if it is supposed to work properly. This was hard to detect for me, since IE didn't give any error messages at all about this.

Anyway, the code that works now actually look like this for the second javascript code:

// For IE4+
ie=&#40;document.all&#41;?true&#58;false;

// For Mozilla
dom=&#40;&#40;document.getElementById&#41; && &#40;!ie&#41;&#41;?true&#58;false;

function setEventByObject&#40;ob, ev, fu&#41; &#123;
   if &#40;dom&#41; &#123;
      ob.addEventListener&#40;ev, fu, false&#41;;
   &#125;
   if &#40;ie&#41; &#123;
      ob.attachEvent&#40;'on' + ev, fu&#41;;
   &#125;
&#125;

function setEventById&#40;id, ev, fu&#41; &#123;
   if &#40;dom&#41; &#123;
      document.getElementById&#40;id&#41;.addEventListener&#40;ev, fu, false&#41;;
   &#125;
   if &#40;ie&#41; &#123;
      document.getElementById&#40;id&#41;.attachEvent&#40;'on' + ev, fu&#41;;
   &#125;
&#125;

// Global variables
var initialString="Testing to insert text into main window...";
var initialTextNode=window.opener.document.createTextNode&#40;initialString&#41;;
var timeNode;
var testNode;

window.opener.document.getElementById&#40;'textString'&#41;.appendChild&#40;initialTextNode&#41;;

function currentTime&#40;&#41; &#123;
   var today=new Date&#40;&#41;;
   if &#40;timeNode==null&#41; &#123;
      timeNode=window.opener.document.createTextNode&#40;today.toLocaleTimeString&#40;&#41;&#41;;
      window.opener.document.getElementById&#40;'timeString'&#41;.appendChild&#40;timeNode&#41;;
   &#125;
   else &#123;
      window.opener.document.getElementById&#40;'timeString'&#41;.removeChild&#40;timeNode&#41;;
      timeNode=window.opener.document.createTextNode&#40;today.toLocaleTimeString&#40;&#41;&#41;;
      window.opener.document.getElementById&#40;'timeString'&#41;.appendChild&#40;timeNode&#41;;
   &#125;
&#125;
window.setInterval&#40;currentTime, 1000&#41;;

function sendTestText&#40;&#41; &#123;
   var testTheString=document.getElementById&#40;'testText'&#41;.value;
   testNode=window.opener.document.createTextNode&#40;testTheString&#41;;
   window.opener.document.getElementById&#40;'testString'&#41;.appendChild&#40;testNode&#41;;
&#125;
setEventById&#40;'button3', 'click', sendTestText&#41;;

/Soo-Im

hi, i have the same problem as you, but i don't understand how do you fix it.
This works fine with firefox, but not in IE that is where has to work (political bussiness:(

I post here my problem...

/function that opens the new window
function openFaqList() {
for (i=0; i<document.sendFaq.isbooked.length;i++) {
if (document.sendFaq.isbooked.checked && document.sendFaq.isbooked.value=='yes')
WindowObjectReference = window.open("faqList.asp?booked=1","FAQ","width=420,height=230,resizable,scrollbars=yes,status=0");
if  (document.sendFaq.isbooked.checked && document.sendFaq.isbooked.value=='no')
WindowObjectReference = window.open("faqList.asp?booked=0","FAQ","width=420,height=230,resizable,scrollbars=yes,status=0");
}
}


... html code ...
--> here the function is called
<a href="javascript:openFaqList()" class="faq-link-2">[Select]</a>
... html code ...



==> code for the new window opened
(javascript section)
var listaFAQs="";
var i;
for (i=0;i<document.selectFAQ.elements.length;i++)
if (document.selectFAQ.elements.type == 'checkbox')  //comprobamos que el objeto es un checkbox
if (document.selectFAQ.elements.checked)  // introducimos en un vector las FAQs seleccionadas
if (listaFAQs == "") {
listaFAQs=listaFAQs+document.selectFAQ.elements.name;
}
else {
listaFAQs=listaFAQs+","+document.selectFAQ.elements.name;
}
// pagina que tratara los datos
alert(listaFAQs);
opener.document.forms[0].action="customer_support.asp?vengode=popup&selectedfaqs="+listaFAQs;
opener.document.forms[0].submit();
// se cierra la ventana
window.close();


...
==> here we want to go back to the parent window with the selection done in this window
response.write("<form name=selectFAQ action='javascript:faq2send_selected();' method='post'>")
cont=1
rs.MoveFirst


while not rs.eof
response.write("<input type='checkbox' value='" & rs("faq_id") & "' name='checkbox_"& rs("faq_id") &"'>"&cont&". "&rs("title_en")&"<br>")
rs.movenext
cont=cont+1
wend
response.write("<INPUT TYPE=SUBMIT VALUE='Hecho'></form>")
...

Thanks !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.