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

How to prevent a single tab from closing?

  • 17 replies
  • 1 has this problem
  • 810 views
  • Last reply by tinker123

more options

Firefox 76.0 64 Bit for Linux Mint 1.0 Mint 19 Cinnamon3.8.9

How can I prevent a single tab from closing?

I would like to prevent the keystroke Ctrl W from closing the active tab.

My understanding is that it is impossible to change keybindings in Firefox.

I thought an alternative might be to set Firefox to warn before closing a tab.

I had no luck in finding such an option.

Is there such an option?

If not, any other ideas?

'''Firefox 76.0 64 Bit for Linux Mint 1.0''' '''Mint 19 Cinnamon3.8.9''' How can I prevent a single tab from closing? I would like to prevent the keystroke Ctrl W from closing the active tab. My understanding is that it is impossible to change keybindings in Firefox. I thought an alternative might be to set Firefox to warn before closing a tab. I had no luck in finding such an option. Is there such an option? If not, any other ideas?

Chosen solution

I found something easy that works.

On a whim I Googled on how to keep a tab open despite Ctrl W being hit for the Chrome browser. I noticed that it also has the same Ctrl W shortcut. I thought it was odd that the same shortcuts would exist. In Chrome you can right click on a tab and "pin" it open. I had the thought that if everyone is following conventions these days maybe Firefox has this too. It does.

If I pin a tab in Firefox the worse that happens when I hit Ctrl W is that Firefox will switch to another open tab leaving the other tab up and alone.

This will prevent my WebVPN connection being lost and my work set up bollixed when I accidentally hit Ctrl W.

Guys, I appreciate your time spent in trying to help me!

Have a good one!

Read this answer in context 👍 1

All Replies (17)

more options

I don't think that it is possible to disable Ctrl+W.

You can always reopen a tab you closed accidentally via "Ctrl+Shift+T".

If you haven't done this yet then you can use this pref to prevent closing Firefox with only one tab present.

  • browser.tabs.closeWindowWithLastTab = false
more options

Hi cor-el,

I appreciate your time.

That does not work.

I set the variable you listed as false.

I pulled a tab out of my existing Firefox window into its own window. Then I closed it. Zero warning.

Thanks for trying.

more options

Perhaps you have occasionally seen a dialog when you want to close a tab asking whether you really want to close it. The page is listening for the beforeunload event and triggering Firefox to display that message.

It's not difficult to inject that kind of script into a page, but if you only want it to fire for Ctrl+w and not for clicking the tab close button, I think there will be a problem because the page doesn't receive that key combination so the script cannot distinguish them.

more options

tinker123 said

cor-el said

If you haven't done this yet then you can use this pref to prevent closing Firefox with only one tab present.
  • browser.tabs.closeWindowWithLastTab = false

I pulled a tab out of my existing Firefox window into its own window. Then I closed it. Zero warning.

It's not intended to give a warning. With that setting, when you close the last tab in a window, Firefox should open a new tab to keep the window open. Then you can undo the close tab.

more options

Hi jscher2000


I typed beforeun into about:config

It looks turned on.

It isn't working.

Thanks for trying.

more options

Hi tinker123, you need an add-on to inject the script into the pages that you want to protect from closing. There is no built-in feature for that. (Of course, keep the default value of dom.disable_beforeunload = false or else the script won't work.)

more options

It is not possible to get a warning with closing a tab via Ctrl+W or disable this keyboard shortcut. That is why I posted some possible workarounds that you can reopen an accidentally closed tab and prevent closing the window accidentally with closing the last tab.

more options

Hi jscher2000

That didn't work either.

Perhaps because I had two firefox windows open: one with a single tab, and another with several tabs.

more options

Guys,

I really appreciate all of you spending your time trying to help me.

Here is why this issue is a problem for me.

I'm working from home through WebVPN. I work on my computer at work, in a Firefox tab at home.

I have an unconscious habit of pressing Ctrl S to save my work.

By accident, I've been hitting Ctrl W.

That has been closing the tab I have been working in, which bollixes up the WebVPN session I was using. It causes problems on my work computer. It takes me ~10 min to recover from all of that. You can see why that would be a problem if it is happening every day, sometimes more than once a day.

Sadly, there doesn't seem to be a way to prevent Firefox from automatically closing the active tab if Ctrl W is activated.

Thanks for trying.

more options

jscher2000 you wrote:

Hi tinker123, you need an add-on to inject the script into the pages that you want to protect from closing. There is no built-in feature for that. (Of course, keep the default value of dom.disable_beforeunload = false or else the script won't work.)

Can you name the extension that will do that?

Thanks much.

more options

Hi tinker123, is it just one tab you need to protect from closing? Try this:

(A) Open the Web Console in the lower part of a tab you can afford to close (Ctrl+Shift+k)

(B) Paste this code next to the line number or the >> and either press Enter or click Run to execute it. The first time, Firefox may ask you to take some action to prove you understand it is dangerous to run scripts from strangers. That is true.

window.addEventListener('beforeunload', function(evt){evt.preventDefault();}, false);

(C) Try Ctrl+w and you should get a popup like this:

If you do not get a prompt, make sure the preference in about:config has its default value of false (as mentioned earlier).

more options

SyntaxError: unexpected token: identifier debugger eval code:1:6

more options

Hmm, line 1 character 6 is the letter w -- that seems normal in the word window. Try again on a clean line?

more options

No snytax error, but I got the message "undefined"

more options

You can possibly use this command in the Browser Console to disable the key_close shortcut.

key = window.document.getElementById("key_close");
if (key && key.hasAttribute("command")){
 key.removeAttribute("command");
 console.log(key);
}

To enable the command line in the Console:

more options

tinker123 said

No snytax error, but I got the message "undefined"

Okay, the command doesn't return a value, so that's normal. And then:

jscher2000 said

(C) Try Ctrl+w and you should get a popup like this:
If you do not get a prompt, make sure the preference in about:config has its default value of false (as mentioned earlier).
more options

Chosen Solution

I found something easy that works.

On a whim I Googled on how to keep a tab open despite Ctrl W being hit for the Chrome browser. I noticed that it also has the same Ctrl W shortcut. I thought it was odd that the same shortcuts would exist. In Chrome you can right click on a tab and "pin" it open. I had the thought that if everyone is following conventions these days maybe Firefox has this too. It does.

If I pin a tab in Firefox the worse that happens when I hit Ctrl W is that Firefox will switch to another open tab leaving the other tab up and alone.

This will prevent my WebVPN connection being lost and my work set up bollixed when I accidentally hit Ctrl W.

Guys, I appreciate your time spent in trying to help me!

Have a good one!