8/12/2023 0 Comments Close tab browser javascriptThis allows you to use the window.close() method directly in your jQuery file as you would any other script.įor example, this script works perfectly with the preference set to true: To enable this setting go to about:config then search and find the dom.allow_scripts_to_close_windows preference and switch it from false to true. So effectively, it cannot close the tab if it would cause window to closes by being the last tab open.įirefox has an advanced setting that you can enable to allow scripts to close windows, effectively enabling the window.close() method. Note: This solution can only close the tab if it is NOT the last tab open however. Var confirm_result = confirm("Are you sure you want to quit?") By using the Chrome plugin TamperMonkey however we can use the window.close() method if you include the // window.close in the UserScript header of TamperMonkey.įor example, my script (which is triggered when a button with id = 'close_page' is clicked and if 'yes' is pressed on the browser popup) looks like: // =UserScript= ![]() Window.close() will work on tabs opened by a script, or by an anchor with target="_blank" (opened in a new tab)īrowser Specific work-arounds: Google Chrome:Ĭhrome does not allow the window.close() script to be to be run and nothing happens if you try to use it. However, most modern browsers no longer support this script, potentially for security reasons. In the past, the jQuery script window.close() was able to close the current tab without a problem on most browsers. Note: These workarounds were done with a locally hosted site in mind, and (with the exception of Edge) require the browser to be specifically configured, so would not be ideal for publicly hosted sites. Sorry for necroposting this, but I recently implemented a locally hosted site that had needed the ability to close the current browser tab and found some interesting workarounds that are not well documented anywhere I could find, so took it on myself to do so. I believe IE will ask the user for confirmation. Firefox disallows you from closing other windows. ![]() If you opened the window with Javascript (via window.open()) then you are allowed to close the window with javascript. Note: there is browser-specific differences with the above. If you really want Yes and No you'll need to create some kind of modal Javascript dialog box. Now the options on the nfirm() dialog box will be OK and Cancel (not Yes and No). ![]() Otherwise the browser will attempt to go to that URL (which it obviously isn't). You return false here to prevent the default behavior for the event.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |