By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,885 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,885 IT Pros & Developers. It's quick & easy.

Parent Window Issue with Ajax

P: 2
Hello, everyone! I'm hoping that someone in this forum will be kind enough to help me out. I am having a Javascript issue related to Ajax and I just can't seem to figure it out. I am currently using the following Javascript code for Ajax purposes and it works perfectly:

Expand|Select|Wrap|Line Numbers
  1. <script language = "javascript">
  2.   var XMLHttpRequestObject = false; 
  3.  
  4.   if (window.XMLHttpRequest) {
  5.     XMLHttpRequestObject = new XMLHttpRequest();
  6.   } else if (window.ActiveXObject) {
  7.     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  8.   }
  9.  
  10.   function getData(dataSource, divID) 
  11.   { 
  12.     if(XMLHttpRequestObject) {
  13.       var obj = document.getElementById(divID); 
  14.       XMLHttpRequestObject.open("GET", dataSource); 
  15.  
  16.       XMLHttpRequestObject.onreadystatechange = function() 
  17.       { 
  18.         if (XMLHttpRequestObject.readyState == 4 && 
  19.           XMLHttpRequestObject.status == 200) { 
  20.             obj.innerHTML = XMLHttpRequestObject.responseText; 
  21.         } 
  22.       } 
  23.  
  24.       XMLHttpRequestObject.send(null); 
  25.     }
  26.   }
  27. </script>
After including the above code in the header of my page, here is a simple example about how I am using this code:

Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type="button" value="Click Here" onclick="getData('/folder/ajax.php', 'targetDiv')" />
  3. </form>
  4.  
  5. <div id="targetDiv">
  6. <p>Fetched data goes here!</p>
  7. </div>
  8.  
Everything I have explained so far works perfectly. The problem is that I have a Javascript popup window that is being created with window.open() and it contains a form to add new data to the page. I would like this popup window to update the 'targetDiv' on the parent window onUnload. I can't figure out the code to make this work. I've been trying variations of the following code by using "window.opener" but it doesn't work:

Expand|Select|Wrap|Line Numbers
  1.   function getData(dataSource, divID) 
  2.   { 
  3.     if(XMLHttpRequestObject) {
  4.       var obj = window.opener.document.getElementById(divID); 
  5.       XMLHttpRequestObject.open("GET", dataSource); 
  6.  
  7.       XMLHttpRequestObject.onreadystatechange = function() 
  8.       { 
  9.         if (XMLHttpRequestObject.readyState == 4 && 
  10.           XMLHttpRequestObject.status == 200) { 
  11.             obj.innerHTML = XMLHttpRequestObject.responseText; 
  12.         } 
  13.       } 
  14.  
  15.       XMLHttpRequestObject.send(null); 
  16.     }
  17.   }
  18.  
Any of your ideas would be greatly appreciated!
Aug 28 '07 #1
Share this Question
Share on Google+
3 Replies


dmjpro
100+
P: 2,476
Hello, everyone! I'm hoping that someone in this forum will be kind enough to help me out. I am having a Javascript issue related to Ajax and I just can't seem to figure it out. I am currently using the following Javascript code for Ajax purposes and it works perfectly:

Expand|Select|Wrap|Line Numbers
  1. <script language = "javascript">
  2.   var XMLHttpRequestObject = false; 
  3.  
  4.   if (window.XMLHttpRequest) {
  5.     XMLHttpRequestObject = new XMLHttpRequest();
  6.   } else if (window.ActiveXObject) {
  7.     XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  8.   }
  9.  
  10.   function getData(dataSource, divID) 
  11.   { 
  12.     if(XMLHttpRequestObject) {
  13.       var obj = document.getElementById(divID); 
  14.       XMLHttpRequestObject.open("GET", dataSource); 
  15.  
  16.       XMLHttpRequestObject.onreadystatechange = function() 
  17.       { 
  18.         if (XMLHttpRequestObject.readyState == 4 && 
  19.           XMLHttpRequestObject.status == 200) { 
  20.             obj.innerHTML = XMLHttpRequestObject.responseText; 
  21.         } 
  22.       } 
  23.  
  24.       XMLHttpRequestObject.send(null); 
  25.     }
  26.   }
  27. </script>
After including the above code in the header of my page, here is a simple example about how I am using this code:

Expand|Select|Wrap|Line Numbers
  1. <form>
  2. <input type="button" value="Click Here" onclick="getData('/folder/ajax.php', 'targetDiv')" />
  3. </form>
  4.  
  5. <div id="targetDiv">
  6. <p>Fetched data goes here!</p>
  7. </div>
  8.  
Everything I have explained so far works perfectly. The problem is that I have a Javascript popup window that is being created with window.open() and it contains a form to add new data to the page. I would like this popup window to update the 'targetDiv' on the parent window onUnload. I can't figure out the code to make this work. I've been trying variations of the following code by using "window.opener" but it doesn't work:

Expand|Select|Wrap|Line Numbers
  1.   function getData(dataSource, divID) 
  2.   { 
  3.     if(XMLHttpRequestObject) {
  4.       var obj = window.opener.document.getElementById(divID); 
  5.       XMLHttpRequestObject.open("GET", dataSource); 
  6.  
  7.       XMLHttpRequestObject.onreadystatechange = function() 
  8.       { 
  9.         if (XMLHttpRequestObject.readyState == 4 && 
  10.           XMLHttpRequestObject.status == 200) { 
  11.             obj.innerHTML = XMLHttpRequestObject.responseText; 
  12.         } 
  13.       } 
  14.  
  15.       XMLHttpRequestObject.send(null); 
  16.     }
  17.   }
  18.  
Any of your ideas would be greatly appreciated!
Welcome to TSDN.
You can use onbeforeunload.
It will work.
Because when the unload event fired then there is no existence of the window is getting unloaded.
That's why it's not working.
Best of luck with your try.

Kind regards,
Dmjpro.
Aug 28 '07 #2

P: 2
You are an absolute lifesaver! It worked!
Aug 28 '07 #3

dmjpro
100+
P: 2,476
You are an absolute lifesaver! It worked!
No need to appreciate Brother.
You are Welcome.
If I didn't answer you there would be somebody else to Answer you.
I am very glad to see that you succeeded.

Kind regards,
Dmjpro.
Aug 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.