Close the browser using javascript from classic asp

Posted at 8:44:29 AM in Web (9) | Read count: 3197

Before I get into this subject, I use to Google my searches for anything asp with a -.net in order to keep out the asp.net groups, but that doesn't appear to work as well anymore. Now the keyword I use is classic asp.

One of my recent problems has been to close out a window that I opened after the user updated some data on. Some explanation... I have a data entry page that pulls up the next record automatically when the user presses the submit button. I supply the data in a window and the user populates fields on the next page, then presses submit and brings up the next record. Naturally, this doesn't allow for the user to go back to fix any mistakes he might have made. To allow the user to go back, I run a second window with a list of the names the user has completed. As each name is completed, the list updates. So if I just finished one that I needed to fix, I'd click the last name on the list and it would pop up an additional screen populated with the data I had just entered. The way this works in chrome, I have main window that I am doing my normal data entry, a separate window that contains a list and if I click on one name in the list, a new window opens up on a tab next to the main window.

My goal was to let the user update the new window and click update, store the corrections and return to the main window with the new window closing automatically.

The problem is that if I put the window.close() on the submit button, I'd never store the data to the database. I didn't want to get into a vbscript, calling a javascript that called a vbscript. My page is a standard classic asp page that loads a form when initially opening the page with the form calling itself to post the data. This is easy enough as checking the state of the form field (in this case the submit buttons state) allows me to load the fields from the database on the first pass and branch to a different function to save the fields to the database on the second pass. The second pass is the key to closing the window. In the logic that saved the data, I used this code that I got from here.

<%
if xyz = "ok" then
%>
<script type="text/javascript">
window.opener="something";
window.close();
</script>
<%
end if
%>

The if then end if logic was replaced by my own requirements, but the javascript portion was entered exactly as it is here. On the first pass, there is no javascript like this on the page. It gets written as the page gets called from the submit button. But it gets written before any HTML headers are ever written. I just write it out right in the middle of my script. This works exactly has I had planned and closes the new window, saving the data and returning focus back to the main window.

The new window is based on the main windows layout, so when the new window closes, it makes you blink a couple of times trying to figure out what just happened. I went back to open the new window to see if the corrected information got saved. I like this better because it allows a data entry person to keep going with minimal mouse or keyboard activity, but if the information was for the casual browser, I'd put a message on the page that said the data was saved successfully with a link to close the window.

Written by Leonard Rogers on Wednesday, July 20, 2011 | Comments (0)


    Name
    URL
    Email
    Email address is not published
    Remember Me
    Comments

    CAPTCHA Reload
    Write the characters in the image above