943,394 Members | Top Members by Rank

Ad:
Jul 28th, 2003
0

JavaScript's window.opener

Expand Post »
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4. <head>
  5. <title>Test first window</title>
  6. <meta http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1" />
  7. </head>
  8. <body>
  9. <h2>Test first window</h2>
  10. <hr />
  11. <div class="left">
  12. <p>
  13. Open a new browser window:
  14. <input id="openNewWindow" type="text" size="55"
  15. value="commsecondwindow.html" />
  16. <input id="button1" type="button" value="Open" />
  17. </p>
  18. <p>
  19. Close the browser window:&nbsp;&nbsp;&nbsp;
  20. <input id="button2" type="button" value="Close" />
  21. </p>
  22. <p id="textString">
  23. </p>
  24. <p id="timeString">
  25. </p>
  26. <p id="testString">
  27. </p>
  28. </div>
  29. <script type="text/javascript" src="commfirstwindow.js"></script>
  30. <noscript><p>Your browser does not support javascript.</p></noscript>
  31. </body>
  32. </html>

...and the javascript for it:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // For IE4+
  2. ie=(document.all)?true:false;
  3.  
  4. // For Mozilla
  5. dom=((document.getElementById) && (!ie))?true:false;
  6.  
  7. function setEventByID(id, ev, fu) {
  8. if (dom) {
  9. document.getElementById(id).addEventListener(ev, fu, false);
  10. }
  11. if (ie) {
  12. document.getElementById(id).attachEvent('on' + ev, fu);
  13. }
  14. }
  15.  
  16.  
  17.  
  18. var newWindow;
  19.  
  20. function openNewWindow() {
  21. var url=document.getElementById('openNewWindow').value;
  22. newWindow=window.open(url, 'newWindow', '', '');
  23. newWindow.creator=self;
  24. }
  25. setEventByID('button1', 'click', openNewWindow);
  26.  
  27.  
  28. // Close the new window
  29.  
  30. function closeWindow() {
  31. if (newWindow!=null) {
  32. newWindow.close();
  33. }
  34. }
  35. setEventByID('button2', 'click', closeWindow);

The second XHTML document:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  4. <head>
  5. <title>Test second window</title>
  6. <meta http-equiv="Content-Type" content="text/xhtml; charset=iso-8859-1" />
  7. </head>
  8. <body>
  9. <h2>Test second window</h2>
  10. <hr />
  11. <div class="left">
  12. <p>
  13. Send a text to the main window:
  14. <input id="testText" type="text" size="55"
  15. value="This is a test text for the main window!" />
  16. <input id="button3" type="button" value="Send text" />
  17. </p>
  18. </div>
  19. <script type="text/javascript" src="commsecondwindow.js"></script>
  20. <noscript><p>Your browser does not support javascript.</p></noscript>
  21. </body>
  22. </html>

...and the javascript code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // For IE4+
  2. ie=(document.all)?true:false;
  3.  
  4. // For Mozilla
  5. dom=((document.getElementById) && (!ie))?true:false;
  6.  
  7. function setEventById(id, ev, fu) {
  8. if (dom) {
  9. document.getElementById(id).addEventListener(ev, fu, false);
  10. }
  11. if (ie) {
  12. document.getElementById(id).attachEvent('on' + ev, fu);
  13. }
  14. }
  15.  
  16. // Global variables
  17. var initialString="Testing to insert text into main window...";
  18. var initialTextNode=document.createTextNode(initialString);
  19. var timeNode;
  20. var testNode;
  21.  
  22.  
  23. window.opener.document.getElementById('textString').appendChild(initialTextNode);
  24.  
  25.  
  26. function currentTime() {
  27. var today=new Date();
  28. if (timeNode==null) {
  29. timeNode=document.createTextNode(today.toLocaleTimeString());
  30. window.opener.document.getElementById('timeString').appendChild(timeNode);
  31. }
  32. else {
  33. window.opener.document.getElementById('timeString').removeChild(timeNode);
  34. timeNode=document.createTextNode(today.toLocaleTimeString());
  35. window.opener.document.getElementById('timeString').appendChild(timeNode);
  36. }
  37. }
  38. window.setInterval(currentTime, 1000);
  39.  
  40. function sendTestText() {
  41. var testTheString=document.getElementById('testText').value;
  42. testNode=document.createTextNode(testTheString);
  43. window.opener.document.getElementById('testString').appendChild(testNode);
  44. }
  45. setEventById('button3', 'click', sendTestText);

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sooim3 is offline Offline
11 posts
since Jun 2003
Jul 29th, 2003
0

Re: JavaScript's window.opener

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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // For IE4+
  2. ie=(document.all)?true:false;
  3.  
  4. // For Mozilla
  5. dom=((document.getElementById) && (!ie))?true:false;
  6.  
  7. function setEventByObject(ob, ev, fu) {
  8. if (dom) {
  9. ob.addEventListener(ev, fu, false);
  10. }
  11. if (ie) {
  12. ob.attachEvent('on' + ev, fu);
  13. }
  14. }
  15.  
  16. function setEventById(id, ev, fu) {
  17. if (dom) {
  18. document.getElementById(id).addEventListener(ev, fu, false);
  19. }
  20. if (ie) {
  21. document.getElementById(id).attachEvent('on' + ev, fu);
  22. }
  23. }
  24.  
  25. // Global variables
  26. var initialString="Testing to insert text into main window...";
  27. var initialTextNode=window.opener.document.createTextNode(initialString);
  28. var timeNode;
  29. var testNode;
  30.  
  31. window.opener.document.getElementById('textString').appendChild(initialTextNode);
  32.  
  33. function currentTime() {
  34. var today=new Date();
  35. if (timeNode==null) {
  36. timeNode=window.opener.document.createTextNode(today.toLocaleTimeString());
  37. window.opener.document.getElementById('timeString').appendChild(timeNode);
  38. }
  39. else {
  40. window.opener.document.getElementById('timeString').removeChild(timeNode);
  41. timeNode=window.opener.document.createTextNode(today.toLocaleTimeString());
  42. window.opener.document.getElementById('timeString').appendChild(timeNode);
  43. }
  44. }
  45. window.setInterval(currentTime, 1000);
  46.  
  47. function sendTestText() {
  48. var testTheString=document.getElementById('testText').value;
  49. testNode=window.opener.document.createTextNode(testTheString);
  50. window.opener.document.getElementById('testString').appendChild(testNode);
  51. }
  52. setEventById('button3', 'click', sendTestText);

/Soo-Im
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sooim3 is offline Offline
11 posts
since Jun 2003
Apr 25th, 2005
0

Re: JavaScript's window.opener

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[i].checked && document.sendFaq.isbooked[i].value=='yes')
WindowObjectReference = window.open("faqList.asp?booked=1","FAQ","width=420,height=230,resizable,scrollbars=yes,status=0");
if (document.sendFaq.isbooked[i].checked && document.sendFaq.isbooked[i].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[i].type == 'checkbox') //comprobamos que el objeto es un checkbox
if (document.selectFAQ.elements[i].checked) // introducimos en un vector las FAQs seleccionadas
if (listaFAQs == "") {
listaFAQs=listaFAQs+document.selectFAQ.elements[i].name;
}
else {
listaFAQs=listaFAQs+","+document.selectFAQ.elements[i].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 !!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
manou is offline Offline
1 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Does Internet Explorer support Java Script ?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: dhtml coding placement for existing nav bars





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC