I'm debugging an SSO app and I need to prevent the window from closing so I can inspect the requests and responses. I tried to disable Javascript but the login flow requires Javascript. Network > Preserve Log doesn't work either. Pinning the tab didn't work either. There are several redirects before it closes. I do not have access to the code of the SSO server. How can I prevent a script from closing the window?
Asked
Active
Viewed 6,517 times
2 Answers
4
I was able to do it by opening the link in an Incognito Window. When it tried to close itself, Chrome gave the warning
Scripts may close only the windows that were opened by it.
I didn't have a script open the window in the first place. Instead I was using target="_blank"
, but Chrome may have equated that with a script. (window.opener
was set.)

Chloe
- 25,162
- 40
- 190
- 357
-
Cool! Thank you for sharing the solution. Please "accept" your answer! – paulsm4 Aug 31 '18 at 02:55
0
See if these links help:
how to block users from closing a window in javascript?
Take a look at onBeforeUnload.
It wont force someone to stay but it will prompt them asking them whether they really want to leave, which is probably the best cross browser solution you can manage. (Similar to this site if you attempt to leave mid-answer.)
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
This link might also apply to your specific use case:

paulsm4
- 114,292
- 17
- 138
- 190
-
1That is pretty much the opposite of what I want. That is to programatically prevent a user from closing a tab, while I want to manually prevent a script from closing the tab. – Chloe Aug 31 '18 at 00:47
-
I tried `window.close = function(){}` but the `window` object is reset after the first redirect. – Chloe Aug 31 '18 at 00:52