Forum Moderators: open

Message Too Old, No Replies

Closing a popup window

how to close an open popup window

         

giggle

5:42 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Hi

Calm down, calm down.

I'm talking about a pop up window we use in our back office online system.

It is appropriate for the system to open a pop up window for staff to enter some additional information to export a customers details.

When the staff member hits enter and system processes it lands at a 'completed' screen. Is it possible, using JavaScript, to automatically close this window?

I tried using the onload="self.close();" but I think that the window is closing before the ASP code is executed.

Thanks

Mick

Expo

7:10 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Don't understand what you mean by closing before the ASP has executed? Javascript is client side so it would render after the ASP.

How does the popup communicate the info to the asp script?

Sounds like a job for AJAX anyway. You could use AJAX to run the ASP silently and then close the popup.

Fotiman

1:53 pm on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As Expo said, the JavaScript is going to execute client side, AFTER your ASP code has already executed.

rocknbil

6:54 pm on Dec 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Calm down, calm down.

Haha, not all pop up windows are evil. :-)

Do it something like this. My ASP is rusty so I'll use "baby-code" for the logic.

if ([some element in the submitted form]) {

--> Do your script functions here

--> Now output this as a response after doing the functions:

<script type="text/javascript">
window.onload=function () { setTimeout('window.close\(\)',5000); };
document.write('Closing window in five seconds, thank you!'); }
</script>
}

else {
output your form here, with "some element in the form" as a trigger for the above action.
}

You can eliminate the setTimeout if you want:

<script type="text/javascript">
window.onload=function () { window.close(); };
</script>

The important part is this must come after your server side script does it's work. You must always return a response or you get a server error or "white page."

giggle

2:17 am on Dec 9, 2009 (gmt 0)

10+ Year Member



Hmmm...pause before closing. Interesting idea Rocknbil. I'll give that a try.

I know what you guys are saying but, when I tested it with an onload="self.close();" I *think* that the window closed before the ASP had completed. Could be wrong there, but I feel better trying the wait 5 seconds version.

Thanks

rocknbil

3:01 am on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I doubt it. It's more likely you were outputting the JS first, or at least a portion of the page. If you do your script functions first, before anything is output to the browser, either <body onload="window.close();"> or the code I posted should work.

vincevincevince

3:21 am on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ASP is server side; it is processed when the page is requested, not when it is rendered.

rocknbil

7:51 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you can't make this error?

<html><head></head>
(javascript code here)
<body>
<%

' do some server side function

Response.write("ending function")
%>
</body></html>

This is what I meant, do all the functions first so the very last thing is to output to browser.

<%
' do some server side function
%>

<html><head></head>
(javascript code here)
<body>
<p>closing window</p>
</body></html>

Fotiman

8:00 pm on Dec 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@rocknbil, even if your JavaScript code appears above the ASP code in the source file, the ASP will always already have executed by the time the page gets back to the browser. Here's how it works:

1. Browser sends HTTP request to server
2. Server sees that it's for an ASP page, and all ASP is processed on the server.
3. The resulting output is sent as the response back to the client.

So, for example, if suppose your markup looked like this:

<html><head>
<script>...</script>
</head>
<body>
<%
Response.write('foo')
%>
</body></html>

Then the HTML response that gets sent back to the browser will look like this:

<html><head>
<script>...</script>
</head>
<body>
foo
</body></html>

The ASP code has already been processed by the time the JavaScript code executes.

rocknbil

4:27 am on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What if your asp process is intensive and it times out?

Fotiman

6:11 pm on Dec 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If it times out, I'm guessing you'd get a 500 Error from the server.

giggle

1:30 am on Dec 11, 2009 (gmt 0)

10+ Year Member



When I first tested the code the ASP didn't complete (evident by the database not being updated correctly). I assumed that maybe it took a while to process the database updates.

Since then I have implemented Rockinbil's delayed timeout and there are no problems and the staff seem happy enough.

I'll just leave it as it is as I have too much to do to revisit old problems!

Thanks