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

Window Open & Close

P: 90
Hi All,
I have to design a web page in which all file(pdf) present in particular folder is visible.Onclicking particular Item,the file should open in new window.
There is link on parent window which show wether the newly open window is still open or closed.
I have implemented the first part through <a href="JavaScript..."..>txt</a>
and able to view the file in new window.
But the second part ,I am not able to implement.
I try with this code

Expand|Select|Wrap|Line Numbers
  1. <script type ="text/javascript" language ="javascript" >
  2. var my_window =null;
  3. function ClosePopup()
  4. {
  5.     if(my_window && my_window.closed)
  6.     {
  7.     alert('Window already closed!');----1
  8.     }
  9.     else if (my_window && my_window.open)
  10.     {
  11.     alert('Close the window!');
  12.     }
  13. }
  14.  
  15.       function TestPopUp(My_Href)
  16.        {
  17.  
  18.       my_window = window.open(My_Href, "mywindow","status=1");
  19.        }
  20.     </script>
.
Every time 1st alert is getting executed irrespective of state of the child window.
Please help.

Manohar
Jan 29 '08 #1
Share this Question
Share on Google+
13 Replies


gits
Expert Mod 5K+
P: 5,331
hi ...

the following check should work for your function:

Expand|Select|Wrap|Line Numbers
  1. if (typeof my_window != 'undefined' && !my_window.closed) {
  2.     alert('Close the window!');
  3. } else {
  4.     alert('Window already closed!');
  5. }
  6.  
kind regards
Jan 29 '08 #2

P: 90
Hi ,
by using above function always else part is geting executed.
Regards,
Manohar

hi ...

the following check should work for your function:

Expand|Select|Wrap|Line Numbers
  1. if (typeof my_window != 'undefined' && !my_window.closed) {
  2.     alert('Close the window!');
  3. } else {
  4.     alert('Window already closed!');
  5. }
  6.  
kind regards
Jan 29 '08 #3

gits
Expert Mod 5K+
P: 5,331
then adapt the call too and use the onclick instead of href:

[HTML]<a href="#" onclick="TestPopUp('your_href');">txt</a>[/HTML]
kind regards
Jan 29 '08 #4

P: 90
Hi ,
I have changed the code and added onclick ="fun(..)".
But the problem is not solved as it every time executing the else part.

Regards,
Manohar


then adapt the call too and use the onclick instead of href:

[HTML]<a href="#" onclick="TestPopUp('your_href');">txt</a>[/HTML]
kind regards
Jan 29 '08 #5

gits
Expert Mod 5K+
P: 5,331
my_window is a global variable in your page? it has to be one! :)
Jan 29 '08 #6

P: 90
my_window is a global variable in your page? it has to be one! :)
Here is the code that I am using.:

Expand|Select|Wrap|Line Numbers
  1. <script type ="text/javascript" language ="javascript" >
  2. var my_window =null;
  3. function ClosePopup()
  4. {
  5.     if(typeof my_window!='undefined' && !my_window.closed)
  6.     {
  7.     alert('Close the window!');
  8.  
  9.     }
  10.     else 
  11.     {
  12.     alert('Window already closed!');
  13.     }
  14. }
  15.  
  16.       function TestPopUp(My_Href)
  17.        { 
  18.       my_window = window.open(My_Href, "mywindow","status=1");
  19.        }
  20.     </script>
my_window is global variable.


Regards,
Manohar
Jan 29 '08 #7

gits
Expert Mod 5K+
P: 5,331
when i simply test that code in FF it works without problems ... please post the page where you call that code ... is something reloaded elsewhere during the operation?

kind regards

ps: sorry i missed that you posted your code and the var declaration already in the original post so that i should have seen that your var is global :)
Jan 29 '08 #8

P: 90
when i simply test that code in FF it works without problems ... please post the page where you call that code ... is something reloaded elsewhere during the operation?

kind regards

ps: sorry i missed that you posted your code and the var declaration already in the original post so that i should have seen that your var is global :)
Here is the page..code

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadPdf.aspx.vb" Inherits="UploadPdf" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Upload PDF</title>
  8.  
  9. </head>
  10. <script type ="text/javascript" language ="javascript" >
  11. var my_window =null;
  12. function ClosePopup()
  13. {
  14.     if(typeof my_window!='undefined' && !my_window.closed)
  15.     {
  16.     alert('Close the window!');
  17.  
  18.     }
  19.     else 
  20.     {
  21.     alert('Window already closed!');
  22.     }
  23. }
  24.  
  25.       function TestPopUp(My_Href)
  26.        { 
  27.       my_window = window.open(My_Href, "mywindow","status=1");
  28.        }
  29.     </script>
  30. <body >
  31.     <form id="form1" runat="server">
  32.     <div>
  33.      <asp:Panel ID="pnlUpload"  runat ="server" GroupingText ="Upload Pdf" Width ="80%" >
  34.       <br />
  35.       <br />
  36.       <div style ="text-align :center ">
  37.        Upload File :<asp:FileUpload ID="fuPDF" runat ="server" />  &nbsp:&nbsp;&nbsp;&nbsp;
  38.        <asp:Button ID="btnUpload" runat ="server" Text ="Upload" />
  39.       </div>
  40.       <br />
  41.       <asp:Label ID="lblStatusfu" runat ="server" ></asp:Label> 
  42.       <br />
  43.      </asp:Panel>
  44.      <br />
  45.      <asp:Panel ID="pnlView" runat ="server" GroupingText ="View Uploaded File" Width ="80%">
  46.       <br />
  47.       <%--<a href="javascript:TestPopUp('Pdf/abc.pdf')">Actual Test</a>
  48.       <a href="javascript:OpenPopup()"  >Click Here</a>--%>
  49.       <a href ="javascript:ClosePopup()">View Status</a>
  50.       <br />
  51.       <div id ="divFileList" runat ="server" ></div>
  52.       <br />
  53.       <asp:Label ID="lblFileStatus" runat ="server" ></asp:Label>
  54.       <br />
  55.      </asp:Panel>
  56.     </div>
  57.  
  58.     </form>
  59. </body>
  60. </html>
  61. Imports System.IO
  62. Partial Class UploadPdf
  63.     Inherits System.Web.UI.Page
  64.  
  65.     Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
  66.         If fuPDF.HasFile Then
  67.  
  68.         End If
  69.     End Sub
  70.  
  71.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  72.         Dim fName As String = ""
  73.         Dim dirInfo As DirectoryInfo = New DirectoryInfo("C:\Gyanendar\WebSites\TestWeb\Pdf")
  74.         Dim file As FileInfo
  75.         For Each file In dirInfo.GetFiles()
  76.             fName += "<a href=""#"" onclick=""TestPopUp('Pdf/" + File.Name + "');"">" + File.Name + "</a>" + "</br>"
  77.         Next
  78.         divFileList.InnerHtml = fName
  79.     End Sub
  80. End Class
.

With simple HTML page it may work.I need it for pdf for which it is not working.

Regards,
Gyanendar
Jan 29 '08 #9

gits
Expert Mod 5K+
P: 5,331
hi ...

i'm not very familiar with asp ... but it should work with any window that you open by a javascript window.open() ... could you please send the source view as it turns out in the browser, i mean the rendered page? ... so i could test it here locally?

kind regards
Jan 29 '08 #10

P: 90
Hi,
Here is the source View Code:

[HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
Upload PDF
</title></head>
<script type ="text/javascript" language ="javascript" >
var my_window =null;
function ClosePopup()
{
if(typeof my_window!='undefined' && !my_window.closed)
{
alert('Close the window!');
}
else
{
alert('Window already closed!');
}
}

function TestPopUp(My_Href)
{
my_window = window.open(My_Href, "mywindow","status=1");

}
</script>
<body >
<form name="form1" method="post" action="UploadPdf.aspx" id="form1" enctype="multipart/form-data">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTIxMTgyNzkwNDMPZBYCAgMPFgIeB2VuY3R5cGUFE21 1bHRpcGFydC9mb3JtLWRhdGEWAgIDD2QWAgIBDxYCHglpbm5lc mh0bWwFvAg8YSBocmVmPSIjIiBvbmNsaWNrPSJUZXN0UG9wVXA oJ1BkZi81NV9MX1AgQWRkcmVzcyBJbmNvcnJlY3QoUmV0dXJuV G9JbnN1cmVkXzA5XzIzKS5wZGYnKTsiPjU1X0xfUCBBZGRyZXN zIEluY29ycmVjdChSZXR1cm5Ub0luc3VyZWRfMDlfMjMpLnBkZ jwvYT48L2JyPjxhIGhyZWY9IiMiIG9uY2xpY2s9IlRlc3RQb3B VcCgnUGRmLzY5X1N1cmNoYXJnZSBtaW5vciB0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Sg4Xzk zKS5wZGY8L2E+PC9icj48YSBocmVmPSIjIiBvbmNsaWNrPSJUZ XN0UG9wVXAoJ1BkZi9hYmMucGRmJyk7Ij5hYmMucGRmPC9hPjw vYnI+ZGSqN0yzTDwMxBKf1w+FgqEHYcTeWw==" />
</div>

<div>
<div id="pnlUpload" style="width:80%;">
<fieldset>
<legend>
Upload Pdf
</legend>
<br />
<br />
<div style ="text-align :center ">
Upload File :<input type="file" name="fuPDF" id="fuPDF" /> &nbsp:&nbsp;&nbsp;&nbsp;
<input type="submit" name="btnUpload" value="Upload" id="btnUpload" />
</div>
<br />
<span id="lblStatusfu"></span>
<br />

</fieldset>
</div>
<br />
<div id="pnlView" style="width:80%;">
<fieldset>
<legend>
View Uploaded File
</legend>
<br />

<a href ="javascript:ClosePopup()">View Status</a>
<br />
<a href ="javascript:my_window.close();">Close it</a>
<br />
<div id="divFileList"><a href="#" onclick="TestPopUp('Pdf/55_L_P Address Incorrect(ReturnToInsured_09_23).pdf');">55_L_P Address Incorrect(ReturnToInsured_09_23).pdf</a></br><a href="#" onclick="TestPopUp('Pdf/69_Surcharge minor to major accident.pdf');">69_Surcharge minor to major accident.pdf</a></br><a href="#" onclick="TestPopUp('Pdf/71A_Reissue Letter (Late Payment) (5_98).pdf');">71A_Reissue Letter (Late Payment) (5_98).pdf</a></br><a href="#" onclick="TestPopUp('Pdf/71B_2nd reissue letter.pdf');">71B_2nd reissue letter.pdf</a></br><a href="#" onclick="TestPopUp('Pdf/74_ First Report Surcharge (10_91).pdf');">74_ First Report Surcharge (10_91).pdf</a></br><a href="#" onclick="TestPopUp('Pdf/79_Policy Cancelled Then Reissued As Non-Standard Auto (4_97).pdf');">79_Policy Cancelled Then Reissued As Non-Standard Auto (4_97).pdf</a></br><a href="#" onclick="TestPopUp('Pdf/82_4-Pay to EFT.pdf');">82_4-Pay to EFT.pdf</a></br><a href="#" onclick="TestPopUp('Pdf/83_CLUE Surcharge(8_93).pdf');">83_CLUE Surcharge(8_93).pdf</a></br><a href="#" onclick="TestPopUp('Pdf/abc.pdf');">abc.pdf</a></br></div>
<br />
<span id="lblFileStatus"></span>
<br />

</fieldset>
</div>
</div>


<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgK6h7viDwLt3oXMA8PGTWm3AzM8jwPkHWured8yOq9P" />
</div></form>
</body>
</html>
[/HTML]

hi ...

i'm not very familiar with asp ... but it should work with any window that you open by a javascript window.open() ... could you please send the source view as it turns out in the browser, i mean the rendered page? ... so i could test it here locally?

kind regards
Jan 30 '08 #11

gits
Expert Mod 5K+
P: 5,331
hi ...

thank you ... i could only discover one error, since my_window is initialized with null we have to check for that accordingly:

Expand|Select|Wrap|Line Numbers
  1. if (my_window != null && !my_window.closed) {
  2.     // code goes here
  3. }
the 'View Status' action works as intended i think ... and gives the correct message with your posted code ... i opened one window and i closed it ... both times it worked correct ... could you tell with which test the error occurs?

kind regards
Jan 30 '08 #12

P: 90
Hi,
Here my requirement is that , I have to get the status of opened window at any point of time(i.e. I have to get the wether the window is open or close.)
With this code I am able to open file in new window.But at any point of time I am not able to find the status of newly opned window(wether it is still open or closed).
The code segement
Expand|Select|Wrap|Line Numbers
  1. function ClosePopup()
  2. {
  3.     if(my_window!=null && !my_window.closed)
  4.     {
  5.     alert('Close the window!');    
  6.     }
  7.     else 
  8.     {
  9.     alert('Window already closed!');
  10.     }
  11. }
always executing else part.
It means !my_window.closed part always returning true irrespective of state of the newly opened window.
This is the main problem I am facing.

Other problem is
I have added one link to close the opend window from parant window
[HTML] <a href ="javascript:my_window.close();">Close it</a>[/HTML]
But whenever I am clicking this link it showing Javascript error (Permission denied)when child window is open.
If I close the child window than it not showing anything(no error).


Regards,
Manohar


hi ...

thank you ... i could only discover one error, since my_window is initialized with null we have to check for that accordingly:

Expand|Select|Wrap|Line Numbers
  1. if (my_window != null && !my_window.closed) {
  2.     // code goes here
  3. }
the 'View Status' action works as intended i think ... and gives the correct message with your posted code ... i opened one window and i closed it ... both times it worked correct ... could you tell with which test the error occurs?

kind regards
Jan 30 '08 #13

gits
Expert Mod 5K+
P: 5,331
as i said ... the page works locally without problems ... is there a postback or something like that when you click the links? i could not reproduce the error ...

1. i open a new window
2. view state alerts -> close this window
3. close the window
4. view state alerts -> window is closed aready

-> so it works ... now when there is a 'silent' pagereload the my_window-variable will be set to null again ... and we loose the reference to the opened window. could this be the case? put an:

Expand|Select|Wrap|Line Numbers
  1. alert(my_window); 
at top of the ClosePopup() function. what does it alert?

kind regards
Jan 30 '08 #14

Post your reply

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