Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

Close a window created with window.open() doesn't work in javascript, in Firefox 76.0.1

  • 9 replies
  • 1 has this problem
  • 49 views
  • Last reply by Umbert

more options

I'm in Firefox 76.0.1 64 bit for Ubuntu 18.04. From a script javascript I open a new tab with mywindow= window.open(). Later when user click a button this page should be closed with mywindow.close() but it doesn't work because the mywindow page results already closed (mywindow.closed results true and though if I run mywindow.close() the page remains open. The page is created with window.open() from the script thereby should be legal to close that page. Here is the code: jsfiddle code I observed that later I allow the popup and I run again the code it works with the jsfiddle. Furthermore others users seem don't have my same problem thus I think could be a setting problem. Flipping dom.allow_scripts_to_close_windows in about:config to true don't change the situation. I have the same problem with the same version of Firefox in Window 10. With the browser Google Chrome the code works properly. Here is the early version of my question on stackoverflow: early question

Kindly someone could point me if this could be a bug or a setting problem or other, and anyway how to fix it?

All Replies (9)

more options

If the original page can no longer see mywindow, perhaps there is a security setting or add-on which is separating Google into a separate browsing context. Does the same problem occur with other sites, or with the same site (i.e., another jsfiddle link)?

Helpful?

more options

Thanks for the answer. Then, the problem occurs when I use the Firefox browser. It doesn't occur when I use the code into the jsfiddle (because jsfiddle use another browser ,different from Firefox). Instead, if you mean change the site, i.e. no http://www.google.com but another I have already tried and the problem is still there. As regards add-on and security restictions I thought so too, but I don't know where to get my hands.

Modified by Umbert

Helpful?

more options

jscher2000 said

If the original page can no longer see mywindow, perhaps there is a security setting or add-on which is separating Google into a separate browsing context. Does the same problem occur with other sites, or with the same site (i.e., another jsfiddle link)?

Thanks for the answer. Then, the problem occurs when I use the Firefox browser. It doesn't occur when I use the code into the jsfiddle (because jsfiddle use another browser ,different from Firefox). Instead, if you mean change the site, i.e. no http://www.google.com but another I have already tried and the problem is still there. As regards add-on and security restictions I thought so too, but I don't know where to get my hands.

Helpful?

more options

I only have the issue mentioned in the StackOverflow thread: the first time, Firefox blocks jsFiddle from opening a popup and I have to grant permission. It works on every test after that. If you gave jsFiddle permission to open popups and it still isn't working for you, it sounds like there is some isolation in effect.

If you haven't already, could you try:

New Profile Test

This takes about 3 minutes, plus the time to test your script(s).

Inside Firefox, type or paste about:profiles in the address bar and press Enter/Return to load it.

Click the "Create a New Profile" button, then click Next. Assign a name like May2020, ignore the option to relocate the profile folder, and click the Finish button.

After creating the profile, scroll down to it and click the Launch profile in new browser button.

Firefox should open a new window that looks like a brand new, uncustomized installation. (Your existing Firefox window(s) should not be affected.) Please ignore any tabs enticing you to connect to a Sync account or to activate extensions found on your system so we can get a clean test.

Does the script work any better in the new profile?

When you are done with the experiment, you can close the extra window without affecting your regular Firefox profile. (May2020 will remain available for future testing.)

Note: if this test changed your default profile, use the Set as Default Profile button for your regular profile to set it back to normal.

Helpful?

more options

jscher2000 said

I only have the issue mentioned in the StackOverflow thread: the first time, Firefox blocks jsFiddle from opening a popup and I have to grant permission. It works on every test after that. If you gave jsFiddle permission to open popups and it still isn't working for you, it sounds like there is some isolation in effect. If you haven't already, could you try: New Profile Test This takes about 3 minutes, plus the time to test your script(s). Inside Firefox, type or paste about:profiles in the address bar and press Enter/Return to load it. Click the "Create a New Profile" button, then click Next. Assign a name like May2020, ignore the option to relocate the profile folder, and click the Finish button. After creating the profile, scroll down to it and click the Launch profile in new browser button. Firefox should open a new window that looks like a brand new, uncustomized installation. (Your existing Firefox window(s) should not be affected.) Please ignore any tabs enticing you to connect to a Sync account or to activate extensions found on your system so we can get a clean test. Does the script work any better in the new profile? When you are done with the experiment, you can close the extra window without affecting your regular Firefox profile. (May2020 will remain available for future testing.) Note: if this test changed your default profile, use the Set as Default Profile button for your regular profile to set it back to normal.

In the stackoverflow thread it's always me, but here I rephrased the problem without promises and more complicated stuff to allow who help me to focus about the problem. As regards to the jsfiddle I have to clarify: when I run the code with the jsfiddle the google page open in the firefox browser but the starting page,i.e. the parent page, is embedded in the jsfiddle browser and for this rason it works. The problem happens when I execute the code directly in the Firefox browser, I mean both the starting/parent page and the child/google page.

I slavishly followed your profile-method but there is always the same problem. But I detected a new little problem: only the first time, when I have to grant the popup permission I observed by means the "console" in web developer tools that "mywindow" is null and I get a TypeError when I do mywindow.closed. And later I always get that mywindow.closed is already true (but the google page is still open, and though if I try to close with mywindow.close() it remains opens).

If you have any other ideas I would be happy to try it

Helpful?

more options

What if you use the two gray buttons near the bottom of this test page:

https://www.jeffersonscher.com/res/popit.html

Very basic, same site (same page):

<p><button onclick="launchPop()">Open a window/tab</button> <button onclick="closePop()">Close that window/tab</button></p>
<script>
var winScripted;
function launchPop(){
  winScripted = window.open('popit.html#usedButton', '_blank');
}
function closePop(){
  try {
    winScripted.close();
  } catch(err) {
    alert('That did not work. Browser says: ' + err);
  }
}
</script>

Helpful?

more options

jscher2000 said

What if you use the two gray buttons near the bottom of this test page: https://www.jeffersonscher.com/res/popit.html Very basic, same site (same page): <p><button onclick="launchPop()">Open a window/tab</button> <button onclick="closePop()">Close that window/tab</button></p>
<script>
var winScripted;
function launchPop(){
  winScripted = window.open('popit.html#usedButton', '_blank');
}
function closePop(){
  try {
    winScripted.close();
  } catch(err) {
    alert('That did not work. Browser says: ' + err);
  }
}
</script>

It works successfully! Now, maybe I can guess the problem. From MDN window.open() documentation:" The returned reference can be used to access properties and methods of the new window as long as it complies with Same-origin policy security requirements." Translated it mean that as long as the parent page and the child page have the same origin (domain+protocol+port) I can use the mywindow reference otherwise I can't. So far I tried different sites: https://www.google.com, https://www.mozilla.org/it/firefox/new/ and so on and all those have a different origin from mine. I tried with two my local page and it works. May be Chrome is more permissive than Firefox. I very appreciated your advices, I was wondering if I can abuse of your skills and if you could suggest me how to approach the case when the 2 pages have two different origins (i.e. like my case, one local page, and another instead https://www.google.com): if it's possible in Firefox, if it's a configuration problem.... Furthermore i don't understand why other people with my same browser Firefox version are able to run successfully the same code with cross-origin-policy limitation with whom I'am in troubles

Helpful?

more options

I think window.close() works cross-site, even if access to the DOM of the popup is blocked. I updated the launchPop() script with a new URL (different host, same server) and it still works for me. I don't know whether I have any special settings.

Helpful?

more options

jscher2000 said

I think window.close() works cross-site, even if access to the DOM of the popup is blocked. I updated the launchPop() script with a new URL (different host, same server) and it still works for me. I don't know whether I have any special settings.

You should be right. Take a look here: "MDN cross-origin policy" in the section "Window". It says that some methods, and between them there is "window.close" work in a cross-origin way . And I'm struggling to understand because in my case doesn't work. However I am grateful to you for your time. I have very appreciated ,and at least I think I identified the problem.

Helpful?

Ask a question

You must log in to your account to reply to posts. Please start a new question, if you do not have an account yet.