I would like to know if there is way to close the parent window when the child window opens. I am using frame currently( and yes i know the hassle of working with frames, but you know clients).So what i am currently trying to do is open a new child window child.php when a linked is clicked in the frame. And once this child window opens in now need to close the entire frame index.php using javascript. Is this possible and please let me know weather this is browser dependent or not.
-
PHP is server-side. It can not open or close browser windows. Did you mean to use Javascript here? – Charles Dec 18 '12 at 06:09
-
Please do your homework before asking a question. What have you tried so far? - and consult manuals firsthand, e.g. [about `window.close()` at MDN](https://developer.mozilla.org/en-US/docs/DOM/window.close) - Possible duplicate of [Why is window.Close() not working in Firefox?](http://stackoverflow.com/q/7510835/367456) – hakre Dec 18 '12 at 11:06
-
@hakre I tired everything i could search from '' and a bunch of other stuff as well the reason i wrote is my query here is because nothing worked the only website that i found working was ( http://www.javascripter.net/faq/closinga.htm) which offered me the result to some degree, thats it – freaky Dec 18 '12 at 11:12
-
1Please write questions in a format that it is useful for future users as well, not not only for yourself currently. That means it is clear which concrete problem you're facing and what you've tried so far (even if it did not work). That is a much better way to ask because it is more concrete. – hakre Dec 18 '12 at 11:15
1 Answers
You can only close a browser window using JavaScript if it was opened with JavaScript window.open(). In some browser implementations (more like in the past but I'm not sure about this), if you attempted to close() a window that wasn't opened by JavaScript, the browser popped up a confirmation dialogue to the user saying that a script wants to close the window.
So the answer is yes and no. No, you cannot do it by default. Modern browsers will simply ignore this with no error. On the other hand, if you really want to do it this way, you should open your main frameset with JavaScript. Like using a loading page that does nothing else, just pops up your frameset with window.open(). Then, if you open further windows from any of your frames, you will be able to close the frameset's window.

- 14,986
- 6
- 37
- 59
-
so what your saying is that i must use 'window.open' with some value in the frameset and then later on then only i can use 'window.close' to close the main frameset when i open a child window . – freaky Dec 18 '12 at 11:07
-
Yes. The proper way is that in your "loader": `var win = window.open(...);` Then you have to call `close()` on `win`. – marekful Dec 18 '12 at 12:17
-
Also, it's worth to note that if you open a window from your frameset (that was opened by JavaScript), you can close the frameset window from the opened one by 'window.opener.close()' rather than the way I suggested above. – marekful Dec 18 '12 at 12:20