Can't close a window in html or JS

How can I close a "child" window opened from a "main" window in html or JS without resorting to asking the user to close the "child" browser window? I think I have tried just about everything. I have tried opening the "child" window by an tag and with JS. I can get the "child" window to open but cannot seem to return to the unchanged "main" page. I know I must be doing something wrong.

Powerbook G4, Mac OS X (10.4.11)

Posted on Oct 11, 2009 5:00 AM

Reply
18 replies

Oct 11, 2009 6:57 AM in response to Douglas McKibbin

First, I don't think you're using the terminology correctly, and that might be part of the problem. A child window is a frame that is a descendant of another frame, frameset or window. If you're opening a page in a new, top-level window, using the JavaScript Window.open() method, that's not a child. It's just a new window object, which may or may not have a name, depending on how you opened it. If this is not what you mean, please clarify.

Any window that is opened in JavaScript can be closed by calling its close() method, either by calling window.close(), self.close(), top.close() or this.close() from within that window's global namespace or by calling the close() method of the window object from the window from which it was opened if a reference to that object is available. To obtain a reference to the window object, you need to set a variable with global visibility to the object returned by the Window.open() method.

If you need to access the window from which the new window was opened from the new window, you can always access it with that window's opener property. Window.opener is available as long as both windows remain open.

If you need to see an example, please describe exactly what you're trying to do, and I'll show you how to do it. There are so many possible permutations it would take a while to give an example of each of them.

Oct 11, 2009 7:29 AM in response to David Livesay

David I have to disagree with you comment about what a child window is. A child widow can also be a new browser widow open from another browser window link. A child window doesn't always pertain to frames. A child window can't close a parent window.

For example:
http://www.plus2net.com/javascript_tutorial/window-parent.php

Message was edited by: David M Brewer

Oct 11, 2009 8:21 AM in response to David M Brewer

That's pretty horrible. People should know better than that. This is such a widespread misconception, about a very important distinction, and I hate to see someone reinforcing it like that.

Do you understand what an object hierarchy is?

Show me an example where you can obtain a reference to a Window.opener with window.parent. (You're not allowed to prototype the Window object--that would be cheating.) You can't do it, because *top-level windows do not have parent properties.* They are the children of no one.

Oct 11, 2009 9:33 AM in response to David Livesay

Davids,
First, thanks for your help. I have spent way too much time on this and have become frustrated.
The main window is a patient entry form(call it the main window). The main window calls a HIPPA page for the patient to read(call it child window#1) using an a href tag. From this child window I want the user to return to the unchanged entry form after they have read the document. The main window also calls a financial agreement(call it child window#2) also using an a href tag to a blank front window that opens child window#2 in JS window.open(child window#2.html). From this window I also want the user to return to the unchanged entry form without having to manually close a browser window. You can find these at augustasurgery.org.
Nothing seems to work to close the child windows, no matter if the child was opened in JS or by an a href tag. The more I dig into this, the more it seems that window.close is limited. In both the child windows, whether in IE, firefox, or safari, the back browser arrow is dimmed.

Oct 11, 2009 10:15 AM in response to Douglas McKibbin

Douglas,

Cool! Patient education! I used to love doing this stuff!

I'm still not clear on what the problem is. Both the HIPAA form (note the correct spelling--it's misspelled on your page) and the financial agreement are opening in new, top-level windows (although a second window opens when I open the financial agreement form, but it's not clear why). In each case I can close the window with a call to window.close(). I just entered "javascript:window.close()" in the address field, hit Return, and voila! The window closes and I'm looking at the window that was behind it. When I closed the financial agreement form the second window was there, but I was able to close it the same way. All you need to do is insert a button, image, or whatever, and have its onclick method call window.close().

If you want to get fancy I can show you how you can make it check the "I have read..." check boxes on that form.

Another way you can do this is to create a new div element in your main window, then append an iframe to it and load your documents into it. If you make the div background black and give it about 80% opacity, the effect is pretty cool. You also don't need to worry about the user doing anything untoward, like getting confused closing the main window.

Oct 11, 2009 10:51 AM in response to David Livesay

David,
OK you'd think I could spell HIPAA correctly. But no. I am obviously not a skilled webpage author either.
The HIPAA document was originally a MS Word document that I converted to html and pasted into an html snippett in iWeb. There is an awful lot of html junk in this child page when you view the source and I wonder if something is interfering with the button function.
I placed the lines
<input type=button onClick="self.close();" value="Return to the Entry form">
and
<input type=button onClick="window.close();" value="Return to the Entry form">

within the body of the html code in the page HIPPAtest which I think is a child form and is called from OpenHIPPAtest. These buttons do not work. I too can close each window by typing the appropriate JS into the address. If you want I can eMail the code for the HIPAA page if you think that would help.
Many thanks,
Doug

Oct 11, 2009 12:21 PM in response to Douglas McKibbin

I don't see those two input elements.

Is there some reason you can't just take all the junk out of there. I don't know if that's your problem, but it doesn't need to be there.

I can set the document's onclick() event handler to call window.close() by entering this:

javascript:document.onclick=function(){window.close()}


and that works, so I can't see why you couldn't do this with any arbitrary element that has an onclick attribute, but there is a lot of stuff in that page that, frankly, I don't know what it's doing because I'd have to read all the scripts. I think you need to see if whatever you're authoring this with will let you just write plain old generic html without adding all the fancy stuff.

You could send me the code, but if it's what's on the server, I can see it already. Unfortunately I have some chores I need to do today, and the wife's not very happy right now, but I can look at it tonight if you're still stuck.

And don't be too hard on yourself. I've been writing HTML since 1993, and I've done a lot of healthcare consulting, patient education, medical simulation, etc., so I really should know this stuff.

Oct 11, 2009 4:09 PM in response to David M Brewer

David,
Don't get in trouble.... not worth it.
Webpage is http://www.augustasurgery.org
The entry forms are New Patient Questionaire and Returning Patient Questionaire
http://www.augustasurgery.org/Site/NewPatientQuestionaire.html
http://www.augustasurgery.org/Site/ReturningPatientQuestionaire.html

The page http://www.augustasurgery.org/Site/OpenHIPPAtest.html is a test entry page that goes just to the HIPAA document as you suggested and contains the two JS tags athe bottom of the page




<input type=button onClick="self.close();" value="Return to the Entry form">
<input type=button onClick="window.close();" value="Return to the Entry form">

</body>

</html>

Thanks for your diligence in pursuing this. You are really above and beyond.
Doug

Oct 11, 2009 4:40 PM in response to Douglas McKibbin

Here's your trouble.

For some reason, the document is inside an iframe in the document in the window. That's not necessary, as far as I can tell, but if it has to be that way, just have the button call top.close(). This will close the top level window no matter how deep your frameset hierarchy. The only exception is if the window you're trying to close contains a page from a different domain that the document that's trying to close it.

Oct 11, 2009 4:45 PM in response to David Livesay

David,
This is the cleaner code for HIPAA test

<html>

<head>
<SCRIPT language=JavaScript>

function winself()
{
self.close();
}

function winclose()
{
window.close();
}
</SCRIPT>
</head>

<body>
The Health Insurance Portability and Accountability Act of 1996 (HIPPA) is a
federal program that requires all medical records and other individually
identifiable health information used or disclosed by Drs. Caulkins, McKibbin
and Ranzini, Inc. (Drs. CMR, Inc.) in any form to be kept properly
confidential. HIPPA gives the patient new rights to understand and control how
health information is used.



We are required by law to maintain the privacy of your protected health
information and to provide you with notice of our legal duties and privacy
practices with respect to protected health information.



Drs. CMR, Inc. may use and disclose health records for each of the following
purposes: treatment, payment and health care operations. Treatment means
providing, coordinating, or managing health care and related services by one or
more health care providers. Payment means such activities as obtaining
reimbursement for services, confirming coverage, billing or collection
activities, and utilization review. Health care operations include the business
aspects of running our practice, such as conducting quality assessment and
improvement activities, auditing functions, cost-management analysis, and customer
service. We may also create and distribute de-identified health information by
removing all references to individually identifiable information. If a
diagnosis of malignancy is made during your treatment by this office, personal
identifying information will be submitted to the Virginia Cancer Registry as is
legally required by Chapter 548 of the Code of Virginia. We may contact you to
provide appointment reminders or information about treatment alternatives or
other health-related benefits and services that may be of interest to you. Any
other uses and disclosures will be made only with your written authorization.
You may revoke such authorization in writing and we are required to honor and
abide by that written request, except to the extent we have already taken
actions relying on your authorization.



You have the following rights with respect to your protected health information,
which you can exercise by presenting a written request to the Privacy Officer
for Drs. CMR, Inc. You may request restrictions on certain uses and disclosures
of protected health information, including those related to disclosures to
family members, other relatives, close personal friends, or any other person
identified by you. We are, however, not required to agree in writing to remove
it. You may request to receive confidential communications of protected health
information from us by alternative means or at alternative locations. You may
inspect copy and amend your protected health information. You may receive an
accounting of disclosures or protected health information. You may obtain a
paper copy of this notice from us upon request.



This notice is updated as of August 11, 2003. We are required to abide by the terms
of the Privacy Policy Notice currently in effect. We reserve the right to
change the terms of our policy and to make the new notice provisions effective
for all protected health information that we maintain. We will post and you may
request a written copy of any revised Privacy Policy Notice from this office.



If you feel that your privacy
protections have been violated, you have the right to file a written complaint
with our office at 70 Medical Center Circle, Suite 308, Fishersville, VA 22939,
(540)932-5909 or with the Department of Health, P.O. Box 2448, Richmond, VA
23218, (804)367-2104



<input type=button onClick="self.close();" value="Return to the Entry form">
<input type=button onClick="window.close();" value="Return to the Entry form">
<input type=button onClick="winself();" value="Close this window">
<input type=button onClick="winclose();" value="Close this window">
</body>

</html>

Doug

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can't close a window in html or JS

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.