Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Window popup closes on opening 2nd excel file

Status
Not open for further replies.

maboo59

Programmer
Mar 21, 2005
18
US
I have a page that generates a report in excel. They page is submitted and the sql query is built in the .cs file, then a javascript function opens a new window sending that query to a page that gets the results and puts the info into a spreadsheet. first time you run it is works fine. if you go back to the parent window and run the report again, sometimes it will close the existing popup window other times it will run the report with no problems.
What could be causing this?
Is it possible to detect if a popup is open and close it before opening a new one?
 
This button will open a new window when clicked. If you click it again it will close the old window and open up a fresh one:
Code:
<script language=javascript>
var newWindow = null;
function openForum() {
   try {
      newWindow.focus(); 
      newWindow.close();
      newWindow = window.open('blah.txt');
   }
   catch(e) { 
      newWindow = window.open('blah.txt');  
   }
}
</script>
<body>
<form name=blahForm>
<input type=button value='open window' onclick='openForum()'>
</form>
</body>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
kaht,
I get an error saying 'newwindow undefined'.
I think it's because the page is submitting to theserver and when it comes back the variable newwindow is no longer defined in the pages scope anymore.
is that correct?

maboo59
 
newWindow will always be defined. If you look at my example it's the very first variable declared. Not to mention it's declared outside the function so it will have global scope.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
Kaht,
Now it says 'newWindow is null or not an object' when I add alert(e.message) inside the catch block, and the window doesn't close.
maboo69
 
maboo59, I'm just shooting in the dark if I can't see any of your code. If I don't know what you're working with all I can do is type up examples, and my example above works just fine if you copy/past it into a new html file.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
kaht,
I appreciate the help: Here's the important code:
<script language=javascript>
var newWindow = null;
function openExcel() {
try {
newWindow.focus();
newWindow.close();
newWindow = window.open('excelPage.aspx');
}
catch(e) {
newWindow = window.open('excelPage.aspx');
}
}
</script>
<body>
<form name=buildquery>
<input type=hidden name=selectStr value='no' runat=server>
<asp:Button ID="btnValidate" Runat="server" Text="Create Report" OnClick="btnValidate_Click">()'>
</form>
</body>
<script language=javascript>
<!--
if (document.Form1.selectStr.value != 'no')
openExcel();
//-->
</script>

When the page first loads the popup will not be opened. user will choose the report parameters and then click button to submit to server and validate their selections.
if all is good then i set 'selectstr' to 'yes' and when the page loads the function openExcel() will be called to open the new window.
I think the roundtrip to the server is causing me to loose the handle on the window.
Thanks for the help.
maboo59
 
If I understand your problem correctly, you're running the page and the excel document opens the first time it's supposed to. Later, you go back to the opener and refresh the page w/o closing the excel window, leaving that page orphaned. If this is the case I would put code in the page in a try{}catch{} clause to close the popup when the page unloads. It's not necessary to put anything in the catch clause because if it tries to close the window and fails then that just means there was no window to close.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
kaht,
That works but only every other time the page loads.
better than nothing.
thanks

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top