d-_-b
d-_-b

Reputation: 23161

Why does <script>window.opener='x';window.close();</script> not work in Firefox

I found this code and have been using this to automatically close the window...It works in Chrome flawlessly,

How can I do this in firefox? Or is there a better way to close a current window?

Upvotes: 0

Views: 2508

Answers (2)

shareef
shareef

Reputation: 9581

you should try this trick

How to close a window or tab in FireFox with Javascript

The first step is to fool the browser into thinking that it was opened with a script...

window.open('','_parent','');

This opens a new page, (non-existent), into a target frame/window, (_parent which of course is the window in which the script is executed), and defines parameters such as window size etc, (in this case none are defined as none are needed). Now that the browser thinks a script opened a page we can quickly close it in the standard way...

window.close();

And there you have it - I told you it was simple! In case you didn't follow that, here is the complete solution in two easy steps:

  1. Copy/paste the following code to the head of your page...
    <script language="javascript" type="text/javascript">
     function closeWindow() {
     window.open('','_parent','');
     window.close();
     }
     </script>
  1. Set your link like this:
<a href="javascript:closeWindow();">Close Window</a>

or like this:
<input type="button" value="Close Window" onclick="closeWindow()"/>

Upvotes: 1

Ray Cheng
Ray Cheng

Reputation: 12566

it doesn't work in FF because it's by design.

you can only close window opened by your script. even if you may find a trick to make it work for now, but once FF patch it in the next version, your script will stop working again.

my suggestion is to alter your page design. maybe uses modal window or a div.

Upvotes: 5

Related Questions